【发布时间】:2017-06-08 07:33:09
【问题描述】:
[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 的自定义类。
现在我正在寻找一种解决方案来找到所有属性的 ColumnName 值。
public static List<T> ExecuteReader<T>(string str)
{
var res = typeof(T);
return new List<T>();
}
我尝试在我的问题上运行一些 Stack Overflow 代码,但效果不佳。我的代码中缺少什么?
【问题讨论】:
-
你尝试了哪个代码?
-
@Fabiano 我又试了一次 typeof(T).GetProperties() return null
-
在您的示例中,您没有属性,只有成员变量。将它们转换为属性或使用 typeof(T).GetMembers()
-
或
typeof(T).GetFields()(它们是字段)。GetMembers()将返回两个字段、属性、方法、事件……
标签: c# data-structures properties