Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear All,

I am facing some silly problem about getting class type using its name stored in a string.

Here is my Case:

I have a HR project Consists of the Following:

1- HR.Core Class Library Project which contains my classes (Employee, Department, ....)
2- HR.Domain .....
3- HR.Repository .....
.....
4- HRApp which is the web application that contains my views, models and cotrollers and this project contains a reference of the other projects.

------------------------
Now in HRApp i want to get Employee Class using its Name which is stored in a string.

I tried Type.GetType("HR.Core.Employee")
But it always returns null.
------------------------

Notice that if i put the same code inside the HR.Core project in some kind mehtod called test, it works successfully.

Can any one tell me whats wrong?
Posted
Updated 16-Dec-15 1:04am
v2
Comments
Sergey Alexandrovich Kryukov 16-Dec-15 2:59am    
First of all, why?
In what assembly did you call Type.GetType?
The problem is pretty simple, but more important thing is: getting any type by its name is a pretty bad idea; the compiler cannot give you any error/warning if the name is wrong.
—SA
_ProgProg_ 16-Dec-15 3:03am    
Actually i am trying to make a method which takes the class name as a string and getting its type for some long logic.
If you want i can explain it.
Sergey Alexandrovich Kryukov 16-Dec-15 11:03am    
You did not answer the question about assembly. The type with the type name you are trying to get, is it the type of the same assembly? Is it a file of some assembly already loaded in current process?
—SA
_ProgProg_ 16-Dec-15 3:12am    
Okay Let me explain my case to you:

I am using entity framework Code First.

Employee Class which is in [HR.Core] Maps a DB Table called Employee and i am already defines its attributes like required, stringlength[200] , ......

Now I want the Employee Model Class [In HRApp] properties attributes to be with the same properties attributes of Employee Core Class without the need to define these attributes on the Employee Model class's properties directly and that is the point. Alot of times it happens that i am forgetting attributes or changing attributes values between Employee Core Class And Employee Model Class.

For Example:

Employee Name Property String Length in Employee Core Class is 200 and in Employee Model Class it is 500. (An Exception Is Thrown as i am trying to store 500 characters in 200 characters column).

So i thought about inheriting Employee Core Class properties attributes to be in Employee Model Class properties Attributes.

i have implemented a custom validation class which named CustomRequired will take for example the class name and property name as strings and these property will be required if the employee class mapped property is required.

Now i am ready to add the [CustomRequired("ClassName", "PropertyName")] attribute on any Model property which will make it required if the mapped one is required also.

Now I hope i have explained my point of view clearly
_ProgProg_ 16-Dec-15 6:43am    
@Sergey Alexandrovich Kryukov
Any Ideas??

1 solution

When using GetType by name you need to give a fuller qualified name.

GetType("MyNamespace.MyClass, MyAssembly")


So

GetType("HR.Core.Employee, HR.Core")

above I am assuming the Employee class is an assembly called HR.Core, but it might not be. Example the project properties of the Core project and it will say what the assembly is called.

However in general it seems you're probably taking the wrong approach. Writing lose code like this to get around you forgetting to do things is rarely a proper solution.
 
Share this answer
 
Comments
_ProgProg_ 16-Dec-15 7:13am    
Thanks alot,
that was helpful

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