While prototyping a generic form generator, I found success when using Type.GetType() to get a System.Type instance. I passed in the string name of the “Type” I wished to inspect, for instance:
Type type = Type.GetType("MyNamespace.MyClass");// List of properties that belong to the given Type PropertyInfo[] itemTypeProperties = type.GetProperties();// Object property PropertyInfo property; // Loop through the properties of the specified Typefor(int i = 0; i < itemTypeProperties.Length; i++ ) { ...make some labels and form fields}
The class MyNamespace.MyClass was defined in a sepecate .cs file within the same project as the Form where I called the above code. Everything worked great!
I wanted to move the class .cs file to its own project as it will be reused by more than one project (aah! good, eh) but, when I did, the above code failed. ...but, for why?!
I checked that the reference was appropriately made to the new project and that the build order and dependencies were correct but I got a null reference after the line:Type type = Type.GetType("MyNamespace.MyClass");
I then tried the following.
// Get an instance of the desired classMyNamespace.MyClass MyInstance = new MyNamespace.MyClass;// Get the Type from the class instanceType type = MyInstance.GetType();// ...continue as before// List of properties that belong to the given Type PropertyInfo[] itemTypeProperties = type.GetProperties();// Object property PropertyInfo property; // Loop through the properties of the specified Typefor(int i = 0; i < itemTypeProperties.Length; i++ ) { ...make some labels and form fields}
For the life of me, I can't figure out why moving a file outside the project causes a failure. I assume there is something I am missing about assembly inspection that is probably quite obvious. If I figure it out, I'll post a follow up. If you figure it out, tell me why I'm dumb
Remember Me
Theme design by Jelle Druyts
Pick a theme: BlogXP calmBlue Candid Blue dasBlog Discreet Blog Blue Elegante essence Just Html Mono Movable Radio Blue Movable Radio Heat orangeCream Portal Project84 Slate Sound Waves Tricoleur