C# Strongly Typed Attribute member to describe property

余生长醉 提交于 2019-12-06 13:27:20

No, it's not possible. You can use typeof for Type name, but have to use a string for the member name. This is as far as you can get:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
 public class MyMeta : Attribute{
   public Type SomeType { get; set; }
   public string PropertyName {get;set;}
   public PropertyInfo Property { get { return /* get the PropertyInfo with reflection */; } } 
 }

public class Example{
  [MyMeta(SomeType = typeof(SomeOtherClass))]   //is strongly typed and get intellisense support...
  public string SomeClassProp { get; set; }
  [MyMeta(SomeType = typeof(SomeOtherClass), PropertyName = "SomeOtherProperty")]
  public string ClassProp2 { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!