C# Strongly Typed Attribute member to describe property

故事扮演 提交于 2019-12-08 03:34:50

问题


I was wondering if it was possible to declare An Attribute property that describes a Property so strong typing is required and ideally that intellisense could used to select the property. Class Types works nicely by declaring the member as Type Type But how to get property as a parameter so that "PropName" is not quoted and is strongly typed?

So far: The Attibute Class and sample usage looks like

 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
 public class MyMeta : Attribute{
   public Type SomeType { get; set; }  // works they Way I like.
   // but now some declaration for a property that triggers strong typing 
   // and ideally intellisense support, 
   public PropertyInfo Property { get; set; }    //almost, no intellisence type.Prop "PropName" is required
   public ? SomeProp {get;set;}  //   <<<<<<< any ideas of nice type to define a property
 }

public class Example{
  [MyMeta(SomeType = typeof(SomeOtherClass))]   //is strongly typed and get intellisense support...
  public string SomeClassProp { get; set; }
  [MyMeta(SomeProp = Class.Member)]   // <<< would be nice....any ideas ?
  public string ClassProp2 { get; set; }
  // instead of
  [MyMeta(SomeProp = typeof(T).GetProperty("name" )]   // ... not so nice
  public string ClassProp3 { get; set; }
}

EDIT: To avoid using property name strings i built a simple tool to keep compile time checks whilst storing and using property names in places as strings.

The idea is that you quickly refer to a property via its type and name with intellisense help and code completion from like resharper. Yet pass in a STRING to a tool.

I use a resharper template with this code shell

string propname =  Utilites.PropNameAsExpr( (SomeType p) => p.SomeProperty )   

which refers to

public class Utilities{
  public static string PropNameAsExpr<TPoco, TProp>(Expression<Func<TPoco, TProp>> prop)
    {
        //var tname = typeof(TPoco);
        var body = prop.Body as System.Linq.Expressions.MemberExpression;
        return body == null ? null : body.Member.Name;
    }
 }

回答1:


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; }
}


来源:https://stackoverflow.com/questions/16872389/c-sharp-strongly-typed-attribute-member-to-describe-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!