从 C# 中的自定义字段属性获取值
今天早上,我开始了我认为使用自定义字段属性的快速练习。在尝试了很多事情并搜索了很多示例(大多数涉及类而不是字段属性)后,我正式陷入困境。
我的代码如下。一个特点是该类是使用类构建器在 FileHelpers 中构建的。我的各种部分成功的尝试确实设法从此类中获取字段名,所以我相信该部分工作正常。
我想要做的(根据代码中的注释)是 a) 运行字段,b) 对于每个字段,查看 DBDataTypeAttribute 属性是否存在,以及 c) 看似最困难的部分 - 从属性获取值(FieldType 字符串)和AllowNulls 布尔值)。
任何意见表示赞赏!
标记
class Program
{
static void Main(string[] args)
{
// Desired output:
System.Type userType = null;
userType = ClassBuilder.ClassFromString(@"
public class ExpenseReport
{
[FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
[DBDataTypeAttribute(FieldType = ""varchar(1000)"", AllowNulls = true)]
public String UniqueID;
[FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
public String ERNum;
}");
object[] attributes;
attributes = userType.GetCustomAttributes(typeof(DBDataTypeAttribute), true);
foreach (Object attribute in attributes)
{
// Would like to be able to ID for each field whether the DBDataTypeAttribute is present, and get the FieldType and AllowNulls Values
DBDataTypeAttribute a = (DBDataTypeAttribute)attribute;
Console.WriteLine("Attribute: ", a.FieldType);
Console.ReadLine();
}
}
}
[AttributeUsage(AttributeTargets.Field)]
public class DBDataTypeAttribute : System.Attribute
{
private string fieldtype;
public string FieldType
{
get { return fieldtype; }
}
private string allownulls;
public string AllowNulls
{
get { return allownulls; }
}
}
This morning I embarked on what I thought would be a quick exercise to use custom field attributes. Having tried many things and searching many examples (most involving class rather than field attributes), I'm officially stuck.
My code is below. One peculiarity is that the class is built in FileHelpers using the classbuilder. My various partially successful attempts did manage to get the fieldnames from this class though, so I believe that part works fine.
What I want to do (per the comment in the code) is a) Run through the fields, b) for each, see if the DBDataTypeAttribute Attribute exists, and c) The seemingly hardest part - getting the values from the attribute (FieldType string, and AllowNulls bool).
Any comments appreciated!
Mark
class Program
{
static void Main(string[] args)
{
// Desired output:
System.Type userType = null;
userType = ClassBuilder.ClassFromString(@"
public class ExpenseReport
{
[FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
[DBDataTypeAttribute(FieldType = ""varchar(1000)"", AllowNulls = true)]
public String UniqueID;
[FieldQuoted('""', QuoteMode.OptionalForRead, MultilineMode.AllowForRead)]
public String ERNum;
}");
object[] attributes;
attributes = userType.GetCustomAttributes(typeof(DBDataTypeAttribute), true);
foreach (Object attribute in attributes)
{
// Would like to be able to ID for each field whether the DBDataTypeAttribute is present, and get the FieldType and AllowNulls Values
DBDataTypeAttribute a = (DBDataTypeAttribute)attribute;
Console.WriteLine("Attribute: ", a.FieldType);
Console.ReadLine();
}
}
}
[AttributeUsage(AttributeTargets.Field)]
public class DBDataTypeAttribute : System.Attribute
{
private string fieldtype;
public string FieldType
{
get { return fieldtype; }
}
private string allownulls;
public string AllowNulls
{
get { return allownulls; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很简单;你必须从字段中获取它们,而不是类型。
Pretty simple; you have to get them from the fields, not the type.