提一个超难的问题(至少我是一点思路都没有)

QQQQXSH 2005-06-09 11:45:08
在运行时,怎么取得未知对象的属性字段等信息?(就像在编辑器中能列出成员列表一样)
可以取得的话,假设有如下情况:
已探知:对象A有public int a字段
那么怎么才能在程序中实现为这个未知对象赋值?

分数不上限(哦对了,倾家荡产为限),看回答情况另外开贴再加,
先谢各位了!
...全文
141 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyun_424 2005-06-09
  • 打赏
  • 举报
回复
利用反射是最佳选择
epimetheus 2005-06-09
  • 打赏
  • 举报
回复
利用反射是最佳选择。请参考MSDN
lovefootball 2005-06-09
  • 打赏
  • 举报
回复
这个是给属性附值

using System;
using System.Reflection;
using System.Globalization;

public class MyClass
{
private string myString;
public MyClass()
{
myString = "Old value";
}
string GetField
{
get
{
return myString;
}
}
}

public class FieldInfo_SetValue
{
public static void Main()
{
try
{
MyClass myObject = new MyClass();
Type myType = Type.GetType("MyClass");
FieldInfo myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance);
// Display the string before applying SetValue to the field.
Console.WriteLine( "\nThe field value of myString is {0}.", myFieldInfo.GetValue(myObject));
// Display the SetValue signature used to set the value of a field.
Console.WriteLine( "Applying SetValue(Object, Object).");
// Change the field value using the SetValue method.
myFieldInfo.SetValue(myObject, "New value");
// Display the string after applying SetValue to the field.
Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
// Set the field value to its old value.
myFieldInfo.SetValue( myObject , "Old value" );
myFieldInfo = myType.GetField("myString", BindingFlags.NonPublic | BindingFlags.Instance);
// Display the string before applying SetValue to the field.
Console.Write( "\nThe field value of mystring is {0}.\n", myFieldInfo.GetValue(myObject));
// Display the SetValue signature used to set the value of a field.
Console.WriteLine("Applying SetValue(Object, Object, BindingFlags, Binder, CultureInfo).");
// Change the field value using the SetValue method.
myFieldInfo.SetValue(myObject, "New value", BindingFlags.Public, null, null);
// Display the string after applying SetValue to the field.
Console.WriteLine( "The field value of mystring is {0}.", myFieldInfo.GetValue(myObject));
}
catch( Exception e )
{
// Any exception generated is displayed.
Console.WriteLine( "Exception: {0}", e.Message );
}
}
}
飞不动 2005-06-09
  • 打赏
  • 举报
回复
反射
Richardhu 2005-06-09
  • 打赏
  • 举报
回复
得到可以参考System.Reflection,下面是部分调用方法,或给属性赋值得例子,你可以以次类推。
System.Reflection.Assembly myass = System.Reflection.Assembly.LoadFrom(strDllfilePath);
object o = myass.CreateInstance(strClassFullName);
//metheod
MethodBase method = o.GetType().GetMethod(strMesthodeName,
BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
object oResult = method.Invoke(o,paramerts);
//property
PropertyInfo property = o.GetType().GetProperty(strPropertyName,
BindingFlags.Instance|BindingFlags.NonPublic|BindingFlags.Public);
object oResult = property.GetValue(o,null);
chenzhanyiczy 2005-06-09
  • 打赏
  • 举报
回复
利用反射,具体你可参考MSDN
lovefootball 2005-06-09
  • 打赏
  • 举报
回复
利用反射,system.reflection
using System;
using System.Reflection;
public class LoadInvoke
{
public static void Main(string[] args)
{
Assembly a = Assembly.LoadFrom(args[0]);
Type[] mytypes = a.GetTypes();
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

foreach(Type t in mytypes)
{
MethodInfo[] mi = t.GetMethods(flags);
Object obj = Activator.CreateInstance(t);

foreach(MethodInfo m in mi)
{
m.Invoke(obj, null);
}
}
}
}

具体的概念在网上查查 反射
xiao_p 2005-06-09
  • 打赏
  • 举报
回复
getType()
不可以吗?
alexxzr 2005-06-09
  • 打赏
  • 举报
回复
学习!
QQQQXSH 2005-06-09
  • 打赏
  • 举报
回复
果然是,
以前瞄到过,
不过没起注意,
看来要认真才行!

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧