// Arguments to pass to internal constructor here in order
var args = new object[] { "name", 10, 20, "someId" };
// You need to leave the (Binder) and (CultureInfo) casts so that C# compiler would call the correct overload for Activator.CreateInstance
var obj = (StreamGroupInfo)Activator.CreateInstance(typeof(StreamGroupInfo), BindingFlags.NonPublic | BindingFlags.Instance, (Binder)null, args, (CultureInfo)null);
// Arguments to pass to internal constructor here in order
var args = new object[] { "name", 10, 20, "someId" };
Binder binder = null;
CultureInfo culture = null;
var obj = (StreamGroupInfo)Activator.CreateInstance(typeof(StreamGroupInfo), BindingFlags.NonPublic | BindingFlags.Instance, binder, args, culture);
1条答案
按热度按时间nxowjjhe1#
可以使用activator.createinstance
请注意,在上面的代码中有两个类型转换似乎不必要(VisualStudio建议您删除它们)。但由于参数的值为空,c#无法知道参数类型。因此,它将采用类型object并调用activator.createinstance(类型type,params object[]args)重载,这将不起作用。您可以按如下所示重写代码,以使visualstudio建议您删除强制转换,并且只需要两个额外的行/变量。