C#强类型属性成员来形容财产 [英] C# Strongly Typed Attribute member to describe property
问题描述
我想知道是否有可能宣布,描述了一个属性,因此强类型是必需的,理想的智能感知可以用来选择一个物业财产属性。类类型通过声明成员作为键入类型
可是如何才能让财产作为参数,以便PROPNAME没有报价且是强类型的作品很好?
到目前为止:本Attibute类和样品的使用看起来像
[AttributeUsage(AttributeTargets.Property,的AllowMultiple = FALSE)]
公共类MyMeta:属性{
公共类型SOMETYPE {搞定;组; } //他们的工作方式,我喜欢。
//但现在一些声明,触发强类型
//和理想的IntelliSense支持,
公众的PropertyInfo财产属性{搞定;组; } //几乎没有intellisence type.PropPROPNAME是必需的
公开? SomeProp {获取;集;} //<<<<<<<好的类型的任何想法来定义属性
}
公共类例子{
[MyMeta(SOMETYPE = typeof运算(SomeOtherClass))] //是强类型并获得IntelliSense支持...
公共字符串SomeClassProp {搞定;组; }
[MyMeta(SomeProp = Class.Member)] //&下;&所述;&下;将是很好....任何想法?
公共字符串ClassProp2 {搞定;组; }
//而不是
[MyMeta(SomeProp = typeof运算(T).GetProperty(名称)] // ...不是很好
公共字符串ClassProp3 {获取;设置; }
}
编辑:
。要避免使用属性名称字符串我建立了一个简单的工具,以保持编译时检查,同时存储和场所作为字符串使用属性名称。
我们的想法是,你很快引用属性通过其类型和名称与智能感知的帮助和代码自动完成从像ReSharper的。然而,通过在一个字符串的工具。
我用与此代码外壳
串PROPNAME一个ReSharper的模板= Utilites.PropNameAsExpr((SOMETYPE p)=> p.SomeProperty)
这是指
公共类公用事业{
公共静态字符串PropNameAsExpr< TPoco,TProp>(表达式来; Func键< TPoco,TProp>>托)
{
// VAR TNAME = typeof运算(TPoco);
变种体= prop.Body作为系统.Linq.Expressions.MemberExpression;
返回身体== NULL?空:body.Member.Name;
}
}
没有, 这是不可能的。您可以使用的typeof
的类型名称,但必须使用一个字符串成员名称。这是就可以得到:
[AttributeUsage(AttributeTargets.Property,的AllowMultiple = FALSE)]
公类MyMeta:属性{
公共类型SOMETYPE {搞定;组; }
公共字符串属性名{获得;设置;}
公众的PropertyInfo财产{{返回/ *与反思得到的PropertyInfo * /; }}
}
公共类例子{
[MyMeta(SOMETYPE = typeof运算(SomeOtherClass))] //是强类型并获得IntelliSense支持...
公共字符串SomeClassProp {搞定;组; }
[MyMeta(SOMETYPE = typeof运算(SomeOtherClass),属性名=SomeOtherProperty)]
公共字符串ClassProp2 {搞定;组; }
}
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;
}
}
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; }
}
这篇关于C#强类型属性成员来形容财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!