I am using C# 4.5 and ASP.NET MVC 5. I have the following:
我正在使用C#4.5和ASP.NET MVC 5.我有以下内容:
[Required(ErrorMessage = "Prop1 is required")]
public string Prop1 { get;set;}
[Required(ErrorMessage = "Prop2 is required")]
public string Prop2 { get;set;}
As you see the error message is the property name plus the " is required" string. What I need is instead of typing the property name and the message for every property, use a generic method composer, that will return the name of the decorated property and a string that I add, something like:
如您所见,错误消息是属性名称加上“是必需的”字符串。我需要的是不是为每个属性键入属性名称和消息,而是使用泛型方法编写器,它将返回装饰属性的名称和我添加的字符串,如:
public string GetMessage()
{
// Caller property should be a variable that is retrieved dynamically
// that holds the name of the property that called the function
return CallerProperty + " is required";
}
so now I can user:
所以现在我可以用户:
[Required(ErrorMessage = GetMessage())]
public string Prop2 { get;set;}
so in brief: How can I know the property name that is decorated by an attribute.
简而言之:我如何知道属性修饰的属性名称。
2 个解决方案
#1
0
Use reflection.
Pseudo
public List<Required> CallerProperty<T>(T source)
{
List<Required> result = new List<Required>();
Type targetInfo = target.GetType();
var propertiesToLoop = source.GetProperties();
foreach (PropertyInfo pi in propertiesToLoop)
{
Required possible = pi.GetCustomAttribute<Required>();
if(possible != null)
{
result.Add(possible);
string name = pi.Name; //This is the property name of the property that has a required attribute
}
}
return result;
}
This is just a demo of how to capture a custom attribute on a property. You have to work out how to manage a list of them, or whatever you need in order to generate the return type you need. Perhaps map it with the "pi.Name" also referenced? I don't know precisely what you need.
这只是一个如何捕获属性上的自定义属性的演示。您必须弄清楚如何管理它们的列表,或者您需要的任何内容,以便生成所需的返回类型。也许用“pi.Name”来映射它?我不知道你需要什么。
#2
0
You can use the "nameof" expression as follows:
您可以使用“nameof”表达式,如下所示:
class Class1
{
[CustomAttr("prop Name: " + nameof(MyProperty))]
public int MyProperty { get; set; }
}
public class CustomAttr : Attribute
{
public CustomAttr(string test)
{
}
}
#1
0
Use reflection.
Pseudo
public List<Required> CallerProperty<T>(T source)
{
List<Required> result = new List<Required>();
Type targetInfo = target.GetType();
var propertiesToLoop = source.GetProperties();
foreach (PropertyInfo pi in propertiesToLoop)
{
Required possible = pi.GetCustomAttribute<Required>();
if(possible != null)
{
result.Add(possible);
string name = pi.Name; //This is the property name of the property that has a required attribute
}
}
return result;
}
This is just a demo of how to capture a custom attribute on a property. You have to work out how to manage a list of them, or whatever you need in order to generate the return type you need. Perhaps map it with the "pi.Name" also referenced? I don't know precisely what you need.
这只是一个如何捕获属性上的自定义属性的演示。您必须弄清楚如何管理它们的列表,或者您需要的任何内容,以便生成所需的返回类型。也许用“pi.Name”来映射它?我不知道你需要什么。
#2
0
You can use the "nameof" expression as follows:
您可以使用“nameof”表达式,如下所示:
class Class1
{
[CustomAttr("prop Name: " + nameof(MyProperty))]
public int MyProperty { get; set; }
}
public class CustomAttr : Attribute
{
public CustomAttr(string test)
{
}
}