用c#如何动态创建类?

友天下@277923869 2005-09-16 12:14:00
类的名称是字符串,如何通过字符串动态创建类?
...全文
1738 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ahcsdn 2006-03-31
  • 打赏
  • 举报
回复
关注
  • 打赏
  • 举报
回复
谢谢大家,linuxyf(率人哥哥) :我没有收到你的邮件呀,麻烦再发一份,谢谢。

另外如何给大家加分呀?
2021‘someday 2005-09-20
  • 打赏
  • 举报
回复
ding
agree_able 2005-09-20
  • 打赏
  • 举报
回复
up
zxilu 2005-09-20
  • 打赏
  • 举报
回复

能否也给我一份?先谢了!
linuxyf 2005-09-20
  • 打赏
  • 举报
回复
楼主,我已经给你发了邮件,关于动态编译的,请查收.
lovewindy 2005-09-17
  • 打赏
  • 举报
回复
用反射,System.Reflection里面有你需要的
pnetsoft 2005-09-16
  • 打赏
  • 举报
回复
using System;
using System.Reflection;

namespace DynamicCall
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
public static void Main(string[] args)
{
Calc calc;

string typeName = typeof (Calc).FullName;

Type type = null;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
type = assembly.GetType(typeName);
if (type != null)
{
break;
}
}

if (type != null)
{
calc = (Calc) type.Assembly.CreateInstance(typeName);
}
else
{
return;
}

BindingFlags flags;

//设置Name属性
flags = BindingFlags.SetProperty;
type.InvokeMember("Name", flags, null, calc, new object[] {"My name is wql"});

//获得Name属性
flags = BindingFlags.GetProperty;
string name = (string) type.InvokeMember("Name", flags, null, calc, null);
Console.WriteLine(name);

//增加事件
flags = BindingFlags.InvokeMethod;
OnAddEvent eventProc = new OnAddEvent(calc_AfterAdd);
type.InvokeMember("add_AfterAdd", flags, null, calc, new object[] {eventProc});

//调用Add方法
int a = 3;
int b = 2;
flags = BindingFlags.InvokeMethod;
type.InvokeMember("Add", flags, null, calc, new object[] {a, b});

//calc["Name"] = "zyn";
//Console.WriteLine(calc.Name);

Console.WriteLine(GetProperty(calc, "Name"));
}

private static void calc_AfterAdd(int a, int b, int result)
{
Console.WriteLine("calc_AfterAdd:a={0},b={1},result={2}", a, b, result);
}

public static object GetProperty(object obj, string propName)
{
BindingFlags flags = BindingFlags.GetProperty;
return obj.GetType().InvokeMember(propName, flags, null, obj, null);
}
}

public delegate void OnAddEvent(int a, int b, int result);

public class Calc
{
private string name;
private int count = 0;

// public string this[string propName]
// {
// get
// {
// BindingFlags flags = BindingFlags.GetProperty;
// string value =(string) this.GetType().InvokeMember(propName, flags, null, this, null);
// return value;
// }
// set
// {
// BindingFlags flags = BindingFlags.SetProperty;
// this.GetType().InvokeMember(propName, flags, null, this, new object[]{value});
// }
// }

public int Add(int a, int b)
{
count ++;
int result = a + b;
this.FireOnAddEvent(a, b, result);
return result;
}

public int Count
{
get
{
return this.count;
}
}

public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}

public event OnAddEvent AfterAdd;

private void FireOnAddEvent(int a, int b, int result)
{
if (this.AfterAdd != null)
{
this.AfterAdd(a, b, result);
}
}

public override string ToString()
{
return string.Format("{0}[{1}]", this.GetType().FullName, this.name);
}

}
}
2021‘someday 2005-09-16
  • 打赏
  • 举报
回复
上面的可以吧
长江支流 2005-09-16
  • 打赏
  • 举报
回复
根据楼主的需求,是用c#如何根据类名动态创建类的实例。

楼主用反射就可以了,所有类在一个程序集中,楼主可查Assembly,它的命名空间为System.Reflection。

下面以实例一个已存在的窗口(类)为例,根据窗口名创建它的实例并调用它的方法。
如在Form1中的按钮中,相显示Form2,如下:

private void btnShow_Click(object sender, System.EventArgs e)
{
Type type = Type.GetType("ReflectionTest.Form2");

System.Reflection.Assembly a = System.Reflection.Assembly.GetAssembly(type);

//根据名称创建对象实例
Object obj = a.CreateInstance("ReflectionTest.Form2");

//调用对象方法或属性等
type.InvokeMember("Show",System.Reflection.BindingFlags.InvokeMethod,null,obj,null);

}
zhaoliang_chen 2005-09-16
  • 打赏
  • 举报
回复
mark
linuxyf 2005-09-16
  • 打赏
  • 举报
回复
动态创建类,动态编译也是可以的,System.CodeDom和System.CodeDom.Compiler命名空间的一些类可以完成你的需要。
linuxyf 2005-09-16
  • 打赏
  • 举报
回复
你的意思应该是动态地创建对象吧?
用反射

assembly.CreateObject()
sdd330 2005-09-16
  • 打赏
  • 举报
回复
using System;

namespace MyClassLib
{
class MyClass{
public int a;

public int GetSum(int x, int y) {
return x + y + a;
}

}
}

class ReflectionHelper {

public static object SetValue(string p_ClassName, string p_FieldName, string p_FieldValue) {
// 加载程序集
Assembly _Assembly = Assembly.LoadFrom("MyClassLib.dll");

// 加载类型
Type _Type = _Assembly.GetType(p_ClassName);

// 创建实例
Object _Object = System.Activator.CreateInstance(_Type);

// 取属性
FieldInfo _FieldInfo = _Type.GetField(p_FieldName);

// 类型转换
Object _Value = System.Convert.ChangeType(p_FieldValue, _FieldInfo.FieldType);

// 设值
_FieldInfo.SetValue(_Object, _Value);

#region 方法调用
// 给参数
System.Type[] _ParamTypes = new System.Type[2];
_ParamTypes[0] = System.Type.GetType("System.Int32");
_ParamTypes[1] = System.Type.GetType("System.Int32");

// 取方法信息
MethodInfo _MethodInfo = _Type.GetMethod("GetSum", _ParamTypes);

// 参数值
Object[] _Params = new Object[2];
_Params[0] = 3;
_Params[1] = 4;

//
Object _ReturnValue = _MethodInfo.Invoke(_Object, _Params);

Console.WriteLine("MyClassLib.MyClass.GetSum(3, 4) returns: {0}", _ReturnValue.ToString());
#endregion

return _Object;
}

static void Main() {
Object o = SetValue("MyClassLib.MyClass","a","12");
Console.WriteLine(o.GetType().Name);
Console.WriteLine(o.GetType().GetField("a").GetValue(o));
Console.WriteLine(o.GetType().GetField("a").GetValue(o).GetType().Name);
Console.ReadLine();
}
}

110,538

社区成员

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

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

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