Con*_*ark 19 reflection .net-4.0 optional-parameters
有没有人知道如何使用只有一个可选参数的构造函数来实例化一个类?
我试过了两个
(T)Activator.CreateInstance(typeof(T), new object[] { Type.Missing });
Run Code Online (Sandbox Code Playgroud)
和
(T)Activator.CreateInstance(typeof(T), new object[] { });
Run Code Online (Sandbox Code Playgroud)
没有成功......我遇到了MissingMethodException
Con*_*ark 30
这有效:
(T)Activator.CreateInstance(typeof(T),
BindingFlags.CreateInstance |
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.OptionalParamBinding,null, new object[] {Type.Missing },CultureInfo.CurrentCulture);
Run Code Online (Sandbox Code Playgroud)