NX二次开发-巧用.NET的反射功能
适用版本:NX 7.0及以上版本

反射是.NET中的重要机制,通过反射,可以在运行时动态获得程序或程序集中每一个类型(包括类、结构、委托、接口和枚举等)的成员和成员的信息。有了反射,即可对每一个类型了如指掌。另外可以直接创建对象,即使这个对象的类型在编译时还不知道;也可以直接调用非公开的任何函数、属性和字段值。
图1
{
continue;
}
string title = expAttribute.Title;
if (string.IsNullOrEmpty(title))
{
title = propertyInfo.Name;
}
//将值赋值到实例对象的属性(Property)
var propertyType = propertyInfo.PropertyType;
if (propertyType == typeof(double))
{
var value = nxObject.GetRealUserAttribute(title);
if (value == null)
{
continue;
}
propertyInfo.SetValue(target, System.Convert.ToDouble(value), new object);
}
else if (propertyType == typeof(int) || propertyType.IsEnum)
{
var value = nxObject.GetIntegerUserAttribute(title);
if (value == null)
{
continue;
}
propertyInfo.SetValue(target, System.Convert.ToInt32(value), new object);
}
else if (propertyType == typeof(string))
{
var value = nxObject.GetStringUserAttribute(title);
if (value == null)
{
continue;
}
propertyInfo.SetValue(target, System.Convert.ToString(value), new object);
}
}
}
图2
免责声明:
1、本站所有资源文章出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
2、本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户擦亮眼睛自行鉴别,做一个有主见和判断力的用户。
3、本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
4、侵权违法和不良信息举报 举报邮箱:cnddit@qq.com
还没有评论,来说两句吧...