Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
C# Activator.CreateInstance() throws exception cannot create abstract class exception in my static class.

my code is

Type t2 = Type.GetType(test);
Object o2 = (Activator.CreateInstance(t2));
Posted
Updated 10-Mar-15 19:47pm
v3
Comments
Kornfeld Eliyahu Peter 10-Mar-15 2:33am    
Please show the class definition code...
Kornfeld Eliyahu Peter 10-Mar-15 2:40am    
It is hardly a class definition :-)
Sergey Alexandrovich Kryukov 10-Mar-15 2:43am    
Didn't I just waste time on writing my Solution 1? After such inquirer's reply, I doubt that even my very detailed explanations can be understood :-(
—SA
Sergey Alexandrovich Kryukov 10-Mar-15 2:46am    
Please see Solution 1.
Also note that 3rd line makes the result of 2nd line totally lost.
I would advise you step back to learning more basic aspects of language and platform; come back to reflection a bit later.
—SA
rhl4569 10-Mar-15 2:55am    
ok. thnak you for your suggestion.

1 solution

What part of this error message can be unclear? Let's see…

In first line, you use this function: https://msdn.microsoft.com/en-us/library/w3f99sx1%28v=vs.110%29.aspx[^].

I have no idea why, but you were lucky enough to have this string recognized as some really existing type. But what type? As the exception tells you, the abstract one. What to do? Whatever you want. 1) leave your code as is: it behaves correctly, giving you quite expected response to your string; 2) change this string to the string representing some non-abstract non-static class name, 3) modify the same class, make it non-abstract and non-static.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900