使用反射获取 属性 属性的原始值
Get raw value of property attribute using Reflection
我需要将一些属性标记为密码,以便自动筛选。我找到了一个标准属性:
[PasswordPropertyText]
public string ThePassword { get; set; }
以下方法检查属性是否存在:
private static bool _isPassword(PropertyInfo p)
{
PasswordPropertyTextAttribute passProp = (PasswordPropertyTextAttribute)p.GetCustomAttribute(typeof(PasswordPropertyTextAttribute));
return (passProp != null); // Additional condition should go here
}
现在我想在这里有我自己的逻辑:
- [PasswordPropertyText] 应该导致 true.
- [PasswordPropertyText(true)] 应该导致 true.
- [PasswordPropertyText(false)] 应该导致 false.
但是当省略参数时PasswordPropertyTextAttribute.Password
的默认值是false。
有什么方法可以获取原始属性值吗?
你所描述的无法使用反射来完成。
反射并不是在查看 代码 所写的那样:它是在查看从该代码生成的模型。由于 PasswordPropertyTextAttribute
的默认构造函数将 false
值传递给另一个构造函数,因此它生成的模型与使用 [PasswordPropertyText(false)]
.
没有区别
如果您想要与 built-in 属性不同的行为,我建议您创建自己的属性,该属性具有您正在寻找的行为。
由于属性构造函数信息也存储为元数据,因此您可以通过调用 GetCustomAttributesData
方法获取所需的信息。看看这个简单的例子:
class Program
{
[PasswordPropertyText]
public string Password1 { get; set; }
[PasswordPropertyText(true)]
public string Password2 { get; set; }
[PasswordPropertyText(false)]
public string Password3 { get; set; }
static void Main(string[] args)
{
var props = typeof(Program).GetProperties();
foreach(var prop in props)
{
var attributeData = prop.GetCustomAttributesData().First(x => x.AttributeType == typeof(PasswordPropertyTextAttribute));
Console.WriteLine($"{prop.Name}: {(attributeData.ConstructorArguments.Cast<CustomAttributeTypedArgument?>().FirstOrDefault()?.Value ?? true)}");
}
Console.ReadLine();
}
}
输出:
Password1: True
Password2: True
Password3: False
我需要将一些属性标记为密码,以便自动筛选。我找到了一个标准属性:
[PasswordPropertyText]
public string ThePassword { get; set; }
以下方法检查属性是否存在:
private static bool _isPassword(PropertyInfo p)
{
PasswordPropertyTextAttribute passProp = (PasswordPropertyTextAttribute)p.GetCustomAttribute(typeof(PasswordPropertyTextAttribute));
return (passProp != null); // Additional condition should go here
}
现在我想在这里有我自己的逻辑:
- [PasswordPropertyText] 应该导致 true.
- [PasswordPropertyText(true)] 应该导致 true.
- [PasswordPropertyText(false)] 应该导致 false.
但是当省略参数时PasswordPropertyTextAttribute.Password
的默认值是false。
有什么方法可以获取原始属性值吗?
你所描述的无法使用反射来完成。
反射并不是在查看 代码 所写的那样:它是在查看从该代码生成的模型。由于 PasswordPropertyTextAttribute
的默认构造函数将 false
值传递给另一个构造函数,因此它生成的模型与使用 [PasswordPropertyText(false)]
.
如果您想要与 built-in 属性不同的行为,我建议您创建自己的属性,该属性具有您正在寻找的行为。
由于属性构造函数信息也存储为元数据,因此您可以通过调用 GetCustomAttributesData
方法获取所需的信息。看看这个简单的例子:
class Program
{
[PasswordPropertyText]
public string Password1 { get; set; }
[PasswordPropertyText(true)]
public string Password2 { get; set; }
[PasswordPropertyText(false)]
public string Password3 { get; set; }
static void Main(string[] args)
{
var props = typeof(Program).GetProperties();
foreach(var prop in props)
{
var attributeData = prop.GetCustomAttributesData().First(x => x.AttributeType == typeof(PasswordPropertyTextAttribute));
Console.WriteLine($"{prop.Name}: {(attributeData.ConstructorArguments.Cast<CustomAttributeTypedArgument?>().FirstOrDefault()?.Value ?? true)}");
}
Console.ReadLine();
}
}
输出:
Password1: True
Password2: True
Password3: False