Har*_*nan 4 c# reflection system.reflection
我有一个不同的场景.我需要创建一个公共类的实例,但它的所有构造函数都是内部的.该类没有默认构造函数.
我尝试了以下方法,但它没有用.
Activator.CreateInstance(typeof(ClassName));
Activator.CreateInstance(typeof(ClassName), nonpublic:true);
Activator.CreateInstance(typeof(ClassName),true);
Activator.CreateInstance(typeof(ClassName), new object[]{double,bool});
Run Code Online (Sandbox Code Playgroud)
我也试过这个,但最终得到了System.MissingMethodException.
var constructors = typeof(ClassName).GetConstructors();
foreach(var ctor in constructors)
ctor.Invoke(new object[] {double, bool});
Run Code Online (Sandbox Code Playgroud)
我无法在Xamrarin中使用BindingFlags.我被卡住,有人有解决方案吗?
我自己找到了解决方案.这就是我做的.
ConstructorInfo info = typeof(ClassName).GetTypeInfo().DeclaredConstructors.First();
ClassName e = info.Invoke(new object[] { parameters }) as ClassName;
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助某人.干杯:)