[英]Reflection Activator.CreateInstance() Saying Variables Are NULL
string to = "user@example.com";
string from = "user@example.com";
string title = "Test";
string body = "Test Email";
Type t1 = Type.GetType("System.Net.Mail.MailMessage");
Object test = Activator.CreateInstance(t1, new object[] { to, from, title, body });
我正在尝试对System.Net.Mail使用反射(不要问为什么)。
现在,当我运行和编译时,它说到,从,标题和正文为NULL(System.ArgumentNullException:值不能为null。)
为什么会这样呢? 如果我用定义的变量替换变量,它就可以正常工作。
编辑1完全错误
System.ArgumentNullException: Value cannot be null.
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at mcclureski.Program.Main(String[] args) in c:\Users\JohnDoe\Documents\SharpDevelop Projects\Test\Program.cs:line 67
Activator.CreateInstance的第二个参数是params数组 ,因此不要实例化对象数组,只需将参数以正确的顺序放置以适合您的类型构造:
Object test = Activator.CreateInstance(t1, to, from, title, body);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.