根据类名className获取属性和属性对应的Description值,给指定对象赋值
public void AddByClassDataItem(string className)
{
Assembly _Assembyle = Assembly.Load("Test.Model");
Type[] _TypeList = _Assembyle.GetTypes();
//Type userClass;
Type type=typeof(DataDictionaryController);
object o = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("setDataItemByUserClass", BindingFlags.Instance | BindingFlags.Public);
for (int i = 0; i < _TypeList.Length; i++)
{
if(_TypeList[i].Name==className)
{
method = method.MakeGenericMethod(_TypeList[i]);
method.Invoke(o, null);
}
}
}
public void setDataItemByUserClass<T>()
{
//ISecurityService securityService = ServiceLocator.Instance.GetService<ISecurityService>();
string result = "";
ANCDataItem dataItem = new ANCDataItem();
foreach (var item in typeof(T).GetProperties())
{
dataItem.Code = item.Name;
if((item.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false)).Length<1) continue;
dataItem.Description = ((System.ComponentModel.DescriptionAttribute[])item.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false))[0].Description;
dataItem.UserClass = typeof(T).Name;
ServiceContext.DataService.SaveData<ANCDataItem>(dataItem);
}
}