如果你不知道 test1 类的定义,那你怎么知道 test1 类会有一个 sarr 的字段呢, 怎么知道他是一个数组类型呢?
解决方案 »
- 请问下c#可以调用别人编写的c++动态库?
- MethodInfo 未将对象引用设置到对象的实例
- 元宵节了献给CSDN的朋友最宝贵的礼物,来自全球最顶尖的IT培训企业-Total Training for Microsoft Visual Studio 2005 中文版视频培训教材
- 在线程中如何打开指定Form窗体
- vs.net2003项目开发完做安装包,如何将项目引用的activex控件一起打包?
- 急!!!一个关于取数的算法问题向各位高手求助~~~~
- C# 怎么连接MYSQL数据库
- 关于其他技术和.net相对应技术的问题。
- ASP.NET 平台下怎样才能实现局域网中不同主机的相同web页面之间发送消息?
- 欢迎讨论,谁有C#或VB.net语言版本的万年历,或是阴阳历转换的源代码,我这找到了一个Javascript版本的。
- 客户端IP问题,求助!!!!!!!!!!!!!!
- 怎么往DataGrid里面添加一列CheckBox?(WinForm中)
Type type=a[1].GetType();
MessageBox.Show (type.ToString ());
//
public object GetValue( string className, string propertyName, object objInstance )
{
Type type = Type.GetType( className, true, true );
PropertyInfo propertyInfo = type.GetProperty( propertyName );
object obj = propertyInfo.GetValue( objInstance, null );
return ( object ) obj;
}
string className = t1.Name;
FieldInfo[] fi = t1.GetFields();
FieldInfo[] ff = t3.GetFields();
for (int i = 0; i < fi.Length; i++)
{
Type tt1 = fi[i].FieldType;
string pp = fi[i].Name;
if (tt1.IsArray)
{
string [] sarr = ( string [] ) GetValue( className, pp, test1 );
} public static object GetValue( string className, string propertyName, object objInstance )
{
Type type = objInstance.GetType();
PropertyInfo propertyInfo = type.GetProperty( propertyName );
object obj = propertyInfo.GetValue( objInstance, null );
return ( object ) obj;
}
运行到这一步出错,PropertyInfo propertyInfo = type.GetProperty( propertyName );大家帮我看看什么原因?
Field 跟 Property 根本不是一回事!// Field
private string myname = String.Empty;// Property
public string Name
{
get { return myname; }
set { myname = value; }
}
2.如果是内部创建的,又是怎么传递到Assembly外部的?如果你的问题是要在 Assembly 外部动态创建 Test1 类的实例,并获取 sarr[] 元素的值,那么就必须事先知道这个类的定义。
object obj = fi[i].GetValue(test1);
string className = t1.Name;
Console.WriteLine(className);
FieldInfo[] fi = t1.GetFields();for (int i = 0; i < fi.Length; i++)
{
Type tt1 = fi[i].FieldType;
if (tt1.IsArray)
{
object [] sarr = (object []) fi[i].GetValue(test1);
for (int j = 0; j < sarr.Length; j++)
{
Console.WriteLine(sarr[0].Equals(sarr[1]));
}
}}
解决如上