【发布时间】:2018-04-10 11:32:58
【问题描述】:
我有一个问题,无法解决: 我做了一个数学模型,它应该设置特定属性“MappingAttribute”的属性值并返回新对象。
问题: 属性值始终设置为默认值“false”。 我哪里错了?
static public T MapToClass<T>(SqlDataReader reader) where T : class
{
T returnedObject = Activator.CreateInstance<T>();
PropertyInfo[] modelProperties = returnedObject.GetType().GetProperties();
for (int i = 0; i < modelProperties.Length; i++)
{
MappingAttribute[] attributes = modelProperties[i].GetCustomAttributes<MappingAttribute>(true).ToArray();
if (attributes.Length > 0) {
attributes[0].AutoIncrement = true;
attributes[0].Primekey = true;
}
}
return returnedObject;
}
【问题讨论】:
-
注意:这些都不是“自定义属性”——它们是“属性”;更改标签/标题
标签: c# custom-attributes