要想创建一个父类相同的类的实例,需要使用 c# 的反射机制。首先需要获取要创建类的 Type 对象,然后使用 Activator.CreateInstance 方法创建实例。
步骤如下:
- 获取父类的 Type 对象;
Type baseType = typeof(BaseClass);
- 使用 Type 对象动态创建子类的 Type 对象;
Type subType = TypeBuilder
.GetTypeBuilder("SubClass")
.CreateType();
- 使用 Activator.CreateInstance 方法创建子类的实例;
object instance = Activator.CreateInstance(subType);
完整代码示例:
using System;
using System.Reflection;
using System.Reflection.Emit;
public class BaseClass
{
public void Print()
{
Console.WriteLine("This is base class.");
}
}
class Program
{
static void Main(string[] args)
{
Type baseType = typeof(BaseClass);
TypeBuilder subTypeBuilder = GetTypeBuilder("SubClass");
subTypeBuilder.SetParent(baseType);
subTypeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
Type subType = subTypeBuilder.CreateType();
object instance = Activator.CreateInstance(subType);
BaseClass baseInstance = instance as BaseClass;
baseInstance.Print();
}
private static TypeBuilder GetTypeBuilder(string className)
{
var assemblyName = new AssemblyName(className);
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType(
className,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
null
);
return typeBuilder;
}
}
在上面的示例中,我们建立了一个名为 SubClass 的动态子类,并将它的父类设为了 BaseClass,并创建了它的实例。执行结果:
This is base class.
示例2:
如果子类的构造函数需要传递参数,还需要使用 CreateInstance 方法的重载函数来传入参数。
完整代码示例
using System;
using System.Reflection;
using System.Reflection.Emit;
public class BaseClass
{
private readonly string message;
public BaseClass(string message)
{
this.message = message;
}
public void Print()
{
Console.WriteLine(message);
}
}
class Program
{
static void Main(string[] args)
{
Type baseType = typeof(BaseClass);
TypeBuilder subTypeBuilder = GetTypeBuilder("SubClass");
ConstructorBuilder constructorBuilder = DefineConstructor(subTypeBuilder);
Type subType = subTypeBuilder.CreateType();
object[] constructorArgs = new object[] { "This is sub class." };
object instance = Activator.CreateInstance(subType, constructorArgs);
BaseClass baseInstance = instance as BaseClass;
baseInstance.Print();
}
private static TypeBuilder GetTypeBuilder(string className)
{
var assemblyName = new AssemblyName(className);
AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
TypeBuilder typeBuilder = moduleBuilder.DefineType(
className,
TypeAttributes.Public |
TypeAttributes.Class |
TypeAttributes.AutoClass |
TypeAttributes.AnsiClass |
TypeAttributes.BeforeFieldInit |
TypeAttributes.AutoLayout,
typeof(BaseClass)
);
return typeBuilder;
}
private static ConstructorBuilder DefineConstructor(TypeBuilder typeBuilder)
{
ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(
MethodAttributes.Public,
CallingConventions.Standard,
new Type[] { typeof(string) }
);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Call, typeof(BaseClass).GetConstructor(new Type[] { typeof(string) }));
ilGenerator.Emit(OpCodes.Ret);
return constructorBuilder;
}
}
在上面的示例中,我们建立了一个名为 SubClass 的动态子类,并将它的父类设为了 BaseClass,SubClass 继承了父类的构造函数,并创建了它的实例。执行结果:
This is sub class.
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:c# 以类名为参创建父类相同的类的实例代码 - Python技术站