[System.AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class ColumnName : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
readonly string Column;
// This is a positional argument
public ColumnName(string columnName)
{
this.Column = columnName;
}
}
public class Comment
{
[ColumnName("ID1")]
public int Id;
[ColumnName("NAME1")]
public string Name;
[ColumnName("TEST1")]
public string Test;
}
在此代码中,您可以看到我创建了具有属性ColumnName的类注释。 ColumnName是我用来定义attirubte的自定义类。如何在C#中获得类属性的属性值
现在我正在寻找一种溶剂来查找所有属性的ColumnName值。
public static List<T> ExecuteReader<T>(string str)
{
var res = typeof(T);
return new List<T>();
}
我试着在我的问题上运行一些堆栈溢出代码,但它不能正常工作。我的代码中缺少什么东西?
你试过了哪些代码? – Fabiano
可能重复[反射 - 获取属性名称和属性值](https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property) – Fabiano
@Fabiano我试过再次typeof(T).GProProperties()返回null – Adrian