C# 如何从类型创建新的对象实例
编译时可能并不总是知道对象的C# 如何从类型创建新的对象实例,c#,.net,performance,reflection,types,C#,.net,Performance,Reflection,Types,编译时可能并不总是知道对象的类型,但可能需要创建类型的实例 如何从类型获取新的对象实例?根系统命名空间中的激活器类功能非常强大 有很多重载用于将参数传递给构造函数等。请访问以下网址查看文档: 或(新路径) 以下是一些简单的例子: ObjectType instance = (ObjectType)Activator.CreateInstance(objectType); ObjectType instance = (ObjectType)Activator.CreateInstance("
类型
,但可能需要创建类型
的实例
如何从
类型
获取新的对象实例?根系统
命名空间中的激活器
类功能非常强大
有很多重载用于将参数传递给构造函数等。请访问以下网址查看文档:
或(新路径)
以下是一些简单的例子:
ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);
ObjectType instance = (ObjectType)Activator.CreateInstance("MyAssembly","MyNamespace.ObjectType");
如果这是为了在应用程序实例中大量调用的东西,那么编译和缓存动态代码比使用activator或
ConstructorInfo.Invoke()
要快得多。动态编译的两个简单选项是编译的或一些简单的。无论哪种方式,当您开始进入紧密循环或多次调用时,差异都是巨大的
ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);
Activator
类有一个通用的变体,使它更容易实现:
ObjectType instance = Activator.CreateInstance<ObjectType>();
ObjectType instance=Activator.CreateInstance();
通用的t t=newt()代码>工作 如果要使用默认构造函数,那么前面介绍的使用System.Activator
的解决方案可能是最方便的。但是,如果类型缺少默认构造函数或必须使用非默认构造函数,则可以选择使用反射或System.ComponentModel.TypeDescriptor
。在反射的情况下,只知道类型名(及其名称空间)就足够了
public AbstractType New
{
get
{
return (AbstractType) Activator.CreateInstance(GetType());
}
}
使用反射的示例:
ObjectType instance =
(ObjectType)System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(
typeName: objectType.FulName, // string including namespace of the type
ignoreCase: false,
bindingAttr: BindingFlags.Default,
binder: null, // use default binder
args: new object[] { args, to, constructor },
culture: null, // use CultureInfo from current thread
activationAttributes: null
);
private T Create<T>() where T : class, new()
{
return new T();
}
使用类型描述符的示例:
ObjectType instance =
(ObjectType)System.ComponentModel.TypeDescriptor.CreateInstance(
provider: null, // use standard type description provider, which uses reflection
objectType: objectType,
argTypes: new Type[] { types, of, args },
args: new object[] { args, to, constructor }
);
这很简单。假设您的类名是Car
,名称空间是Vehicles
,然后将参数作为Vehicles.Car
传递,它返回类型为Car
的对象。像这样,您可以动态创建任何类的任何实例
public object GetInstance(string strNamesapace)
{
Type t = Type.GetType(strNamesapace);
return Activator.CreateInstance(t);
}
如果您的(即本例中的Vehicles.Car
)位于另一个部件中,Type.GetType
将为空。在这种情况下,您需要遍历所有程序集并找到类型
。为此,您可以使用以下代码
public object GetInstance(string strFullyQualifiedName)
{
Type type = Type.GetType(strFullyQualifiedName);
if (type != null)
return Activator.CreateInstance(type);
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
type = asm.GetType(strFullyQualifiedName);
if (type != null)
return Activator.CreateInstance(type);
}
return null;
}
您可以通过调用上述方法来获取实例
object objClassInstance = GetInstance("Vehicles.Car");
我可以跨越这个问题,因为我想为任意类实现一个简单的CloneObject方法(使用默认构造函数)
使用泛型方法,可以要求类型实现New()
对于非泛型,假设该类型具有默认构造函数和catch
如果没有,则为异常
Public Function CloneObject(ByVal src As Object) As Object
Dim result As Object = Nothing
Dim cloneable As ICloneable
Try
cloneable = TryCast(src, ICloneable)
If cloneable IsNot Nothing Then
result = cloneable.Clone()
Else
result = Activator.CreateInstance(src.GetType())
CopySimpleProperties(src, result, Nothing, "clone")
End If
Catch ex As Exception
Trace.WriteLine("!!! CloneObject(): " & ex.Message)
End Try
Return result
End Function
编译表达式是最好的方式!(为了提高性能,可以在运行时重复创建实例)
统计数据(2015年,.net 4.5x64):
统计数据(2015年,.net 4.5,x86):
统计数据(2017年,LINQPad 5.22.02/x64/.NET 4.6):
统计数据(2019年,x64/.NET 4.8):
统计数据(2019年,x64/.NET Core 3.0):
完整代码:
static X CreateY_New()
{
return new Y();
}
static X CreateY_New_Arg(int z)
{
return new Y(z);
}
static X CreateY_CreateInstance()
{
return (X)Activator.CreateInstance(typeof(Y));
}
static X CreateY_CreateInstance_String()
{
return (X)Activator.CreateInstance("Program", "Y").Unwrap();
}
static X CreateY_CreateInstance_Arg(int z)
{
return (X)Activator.CreateInstance(typeof(Y), new object[] { z, });
}
private static readonly System.Reflection.ConstructorInfo YConstructor =
typeof(Y).GetConstructor(Type.EmptyTypes);
private static readonly object[] Empty = new object[] { };
static X CreateY_Invoke()
{
return (X)YConstructor.Invoke(Empty);
}
private static readonly System.Reflection.ConstructorInfo YConstructor_Arg =
typeof(Y).GetConstructor(new[] { typeof(int), });
static X CreateY_Invoke_Arg(int z)
{
return (X)YConstructor_Arg.Invoke(new object[] { z, });
}
private static readonly Func<X> YCreator = Expression.Lambda<Func<X>>(
Expression.New(typeof(Y).GetConstructor(Type.EmptyTypes))
).Compile();
static X CreateY_CompiledExpression()
{
return YCreator();
}
private static readonly Func<X> YCreator_Type = Expression.Lambda<Func<X>>(
Expression.New(typeof(Y))
).Compile();
static X CreateY_CompiledExpression_Type()
{
return YCreator_Type();
}
private static readonly ParameterExpression YCreator_Arg_Param = Expression.Parameter(typeof(int), "z");
private static readonly Func<int, X> YCreator_Arg = Expression.Lambda<Func<int, X>>(
Expression.New(typeof(Y).GetConstructor(new[] { typeof(int), }), new[] { YCreator_Arg_Param, }),
YCreator_Arg_Param
).Compile();
static X CreateY_CompiledExpression_Arg(int z)
{
return YCreator_Arg(z);
}
static void Main(string[] args)
{
const int iterations = 5000000;
Console.WriteLine("Iterations: {0}", iterations);
Console.WriteLine("No args");
foreach (var creatorInfo in new[]
{
new {Name = "Activator.CreateInstance(string assemblyName, string typeName)", Creator = (Func<X>)CreateY_CreateInstance},
new {Name = "Activator.CreateInstance(Type type)", Creator = (Func<X>)CreateY_CreateInstance},
new {Name = "ConstructorInfo.Invoke", Creator = (Func<X>)CreateY_Invoke},
new {Name = "Compiled expression", Creator = (Func<X>)CreateY_CompiledExpression},
new {Name = "Compiled expression (type)", Creator = (Func<X>)CreateY_CompiledExpression_Type},
new {Name = "new", Creator = (Func<X>)CreateY_New},
})
{
var creator = creatorInfo.Creator;
var sum = 0;
for (var i = 0; i < 1000; i++)
sum += creator().Z;
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < iterations; ++i)
{
var x = creator();
sum += x.Z;
}
stopwatch.Stop();
Console.WriteLine("{0}, {1}", stopwatch.Elapsed, creatorInfo.Name);
}
Console.WriteLine("Single arg");
foreach (var creatorInfo in new[]
{
new {Name = "Activator.CreateInstance(Type type)", Creator = (Func<int, X>)CreateY_CreateInstance_Arg},
new {Name = "ConstructorInfo.Invoke", Creator = (Func<int, X>)CreateY_Invoke_Arg},
new {Name = "Compiled expression", Creator = (Func<int, X>)CreateY_CompiledExpression_Arg},
new {Name = "new", Creator = (Func<int, X>)CreateY_New_Arg},
})
{
var creator = creatorInfo.Creator;
var sum = 0;
for (var i = 0; i < 1000; i++)
sum += creator(i).Z;
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < iterations; ++i)
{
var x = creator(i);
sum += x.Z;
}
stopwatch.Stop();
Console.WriteLine("{0}, {1}", stopwatch.Elapsed, creatorInfo.Name);
}
}
public class X
{
public X() { }
public X(int z) { this.Z = z; }
public int Z;
}
public class Y : X
{
public Y() {}
public Y(int z) : base(z) {}
}
static X CreateY_New()
{
返回新的Y();
}
静态X创建新参数(整数z)
{
返回新的Y(z);
}
静态X CreateY_CreateInstance()
{
返回(X)Activator.CreateInstance(typeof(Y));
}
静态X CreateY_CreateInstance_String()
{
返回(X)Activator.CreateInstance(“程序”,“Y”).Unwrap();
}
静态X CreateY_CreateInstance_Arg(int z)
{
返回(X)Activator.CreateInstance(typeof(Y),新对象[]{z,});
}
私有静态只读System.Reflection.ConstructorInfo YConstructor=
typeof(Y).GetConstructor(Type.EmptyTypes);
私有静态只读对象[]空=新对象[]{};
静态X CreateY_Invoke()
{
return(X)YConstructor.Invoke(空);
}
私有静态只读System.Reflection.ConstructorInfo YConstructor_Arg=
typeof(Y).GetConstructor(新[]{typeof(int),});
静态X CreateY_Invoke_Arg(intz)
{
返回(X)YConstructor_Arg.Invoke(新对象[]{z,});
}
私有静态只读Func YCreator=Expression.Lambda(
Expression.New(typeof(Y).GetConstructor(Type.EmptyTypes))
).Compile();
静态X CreateY_CompiledExpression()
{
返回YCreator();
}
私有静态只读Func YCreator_Type=Expression.Lambda(
新表达式(Y型)
).Compile();
静态X CreateY_编译表达式_Type()
{
返回YCreator_Type();
}
私有静态只读参数Expression YCreator_Arg_Param=Expression.Parameter(typeof(int),“z”);
私有静态只读Func YCreator_Arg=Expression.Lambda(
Expression.New(typeof(Y).GetConstructor(New[]{typeof(int),}),New[]{YCreator_Arg_Param,}),
YCreator_Arg_Param
).Compile();
静态X CreateY_编译表达式_Arg(int z)
{
返回YCreator_Arg(z);
}
静态void Main(字符串[]参数)
{
const int迭代次数=5000000;
WriteLine(“迭代:{0}”,迭代);
Console.WriteLine(“无参数”);
foreach(新版本中的var creatorInfo[]
{
新建{Name=“Activator.CreateInstance(string assemblyName,string typeName)”,Creator=(Func)CreateY_CreateInstance},
新建{Name=“Activator.CreateInstance(Type类型)”,Creator=(Func)CreateY_CreateInstance},
新建{Name=“ConstructorInfo.Invoke”,Creator=(Func)CreateY_Invoke},
新的{Name=“Compiled expression”,Creator=(Func)CreateY_Compiled expression},
新建{Name=“Compiled expression(type)”,Creator=(Func)CreateY\u Compiled expression\u type},
新建{Name=“new”,Creator=(Func)CreateY_new},
})
{
var creator=creatorInfo.creator;
var总和=0;
对于(变量i=0;i<1000;i++)
sum+=creator().Z;
var stopwatch=新秒表();
秒表。开始();
对于(变量i=0;i Iterations: 5000000
00:00:00.8481762, Activator.CreateInstance(string, string)
00:00:00.8416930, Activator.CreateInstance(type)
00:00:06.6236752, ConstructorInfo.Invoke
00:00:00.1776255, Compiled expression
00:00:00.0462197, new
Iterations: 5000000
00:00:00.2659981, Activator.CreateInstance(string, string)
00:00:00.2603770, Activator.CreateInstance(type)
00:00:00.7478936, ConstructorInfo.Invoke
00:00:00.0700757, Compiled expression
00:00:00.0286710, new
Iterations: 5000000
00:00:00.3541501, Activator.CreateInstance(string, string)
00:00:00.3686861, Activator.CreateInstance(type)
00:00:00.9492354, ConstructorInfo.Invoke
00:00:00.0719072, Compiled expression
00:00:00.0229387, new
Iterations: 5000000
No args
00:00:00.3897563, Activator.CreateInstance(string assemblyName, string typeName)
00:00:00.3500748, Activator.CreateInstance(Type type)
00:00:01.0100714, ConstructorInfo.Invoke
00:00:00.1375767, Compiled expression
00:00:00.1337920, Compiled expression (type)
00:00:00.0593664, new
Single arg
00:00:03.9300630, Activator.CreateInstance(Type type)
00:00:01.3881770, ConstructorInfo.Invoke
00:00:00.1425534, Compiled expression
00:00:00.0717409, new
Iterations: 5000000
No args
00:00:00.3287835, Activator.CreateInstance(string assemblyName, string typeName)
00:00:00.3122015, Activator.CreateInstance(Type type)
00:00:00.8035712, ConstructorInfo.Invoke
00:00:00.0692854, Compiled expression
00:00:00.0662223, Compiled expression (type)
00:00:00.0337862, new
Single arg
00:00:03.8081959, Activator.CreateInstance(Type type)
00:00:01.2507642, ConstructorInfo.Invoke
00:00:00.0671756, Compiled expression
00:00:00.0301489, new
Iterations: 5000000
No args
00:00:00.3226895, Activator.CreateInstance(string assemblyName, string typeName)
00:00:00.2786803, Activator.CreateInstance(Type type)
00:00:00.6183554, ConstructorInfo.Invoke
00:00:00.0483217, Compiled expression
00:00:00.0485119, Compiled expression (type)
00:00:00.0434534, new
Single arg
00:00:03.4389401, Activator.CreateInstance(Type type)
00:00:01.0803609, ConstructorInfo.Invoke
00:00:00.0554756, Compiled expression
00:00:00.0462232, new
static X CreateY_New()
{
return new Y();
}
static X CreateY_New_Arg(int z)
{
return new Y(z);
}
static X CreateY_CreateInstance()
{
return (X)Activator.CreateInstance(typeof(Y));
}
static X CreateY_CreateInstance_String()
{
return (X)Activator.CreateInstance("Program", "Y").Unwrap();
}
static X CreateY_CreateInstance_Arg(int z)
{
return (X)Activator.CreateInstance(typeof(Y), new object[] { z, });
}
private static readonly System.Reflection.ConstructorInfo YConstructor =
typeof(Y).GetConstructor(Type.EmptyTypes);
private static readonly object[] Empty = new object[] { };
static X CreateY_Invoke()
{
return (X)YConstructor.Invoke(Empty);
}
private static readonly System.Reflection.ConstructorInfo YConstructor_Arg =
typeof(Y).GetConstructor(new[] { typeof(int), });
static X CreateY_Invoke_Arg(int z)
{
return (X)YConstructor_Arg.Invoke(new object[] { z, });
}
private static readonly Func<X> YCreator = Expression.Lambda<Func<X>>(
Expression.New(typeof(Y).GetConstructor(Type.EmptyTypes))
).Compile();
static X CreateY_CompiledExpression()
{
return YCreator();
}
private static readonly Func<X> YCreator_Type = Expression.Lambda<Func<X>>(
Expression.New(typeof(Y))
).Compile();
static X CreateY_CompiledExpression_Type()
{
return YCreator_Type();
}
private static readonly ParameterExpression YCreator_Arg_Param = Expression.Parameter(typeof(int), "z");
private static readonly Func<int, X> YCreator_Arg = Expression.Lambda<Func<int, X>>(
Expression.New(typeof(Y).GetConstructor(new[] { typeof(int), }), new[] { YCreator_Arg_Param, }),
YCreator_Arg_Param
).Compile();
static X CreateY_CompiledExpression_Arg(int z)
{
return YCreator_Arg(z);
}
static void Main(string[] args)
{
const int iterations = 5000000;
Console.WriteLine("Iterations: {0}", iterations);
Console.WriteLine("No args");
foreach (var creatorInfo in new[]
{
new {Name = "Activator.CreateInstance(string assemblyName, string typeName)", Creator = (Func<X>)CreateY_CreateInstance},
new {Name = "Activator.CreateInstance(Type type)", Creator = (Func<X>)CreateY_CreateInstance},
new {Name = "ConstructorInfo.Invoke", Creator = (Func<X>)CreateY_Invoke},
new {Name = "Compiled expression", Creator = (Func<X>)CreateY_CompiledExpression},
new {Name = "Compiled expression (type)", Creator = (Func<X>)CreateY_CompiledExpression_Type},
new {Name = "new", Creator = (Func<X>)CreateY_New},
})
{
var creator = creatorInfo.Creator;
var sum = 0;
for (var i = 0; i < 1000; i++)
sum += creator().Z;
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < iterations; ++i)
{
var x = creator();
sum += x.Z;
}
stopwatch.Stop();
Console.WriteLine("{0}, {1}", stopwatch.Elapsed, creatorInfo.Name);
}
Console.WriteLine("Single arg");
foreach (var creatorInfo in new[]
{
new {Name = "Activator.CreateInstance(Type type)", Creator = (Func<int, X>)CreateY_CreateInstance_Arg},
new {Name = "ConstructorInfo.Invoke", Creator = (Func<int, X>)CreateY_Invoke_Arg},
new {Name = "Compiled expression", Creator = (Func<int, X>)CreateY_CompiledExpression_Arg},
new {Name = "new", Creator = (Func<int, X>)CreateY_New_Arg},
})
{
var creator = creatorInfo.Creator;
var sum = 0;
for (var i = 0; i < 1000; i++)
sum += creator(i).Z;
var stopwatch = new Stopwatch();
stopwatch.Start();
for (var i = 0; i < iterations; ++i)
{
var x = creator(i);
sum += x.Z;
}
stopwatch.Stop();
Console.WriteLine("{0}, {1}", stopwatch.Elapsed, creatorInfo.Name);
}
}
public class X
{
public X() { }
public X(int z) { this.Z = z; }
public int Z;
}
public class Y : X
{
public Y() {}
public Y(int z) : base(z) {}
}
private T Create<T>() where T : class, new()
{
return new T();
}
System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject()
ObjectType instance = (ObjectType)Activator.CreateInstance(objectType);