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 });
I'm trying to use reflection (don't ask why) for System.Net.Mail.
Right now when I run and compile, it says to, from, title, and body are NULL (System.ArgumentNullException: Value cannot be null.)
Why is this happening? and if I replace the variables with what they are defined with it works fine.
EDIT 1 Full Error
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.