Thursday, July 9, 2009

C# class help?

I would like to create a new instance of a class, but I would like the user to tell me the name of the object they want to create, and the code uses that (stored as a string) name and creates the object.


This is done as a method. This is what I thought about, but it creates it called the name of the variable:





public void createTextBox(string textBoxName)


{


// Create a new text box


TextBox = textBoxName new TextBox;


}

C# class help?
The reflection system in .NET is used to interrogate types in the type system as well as to create code on the fly.





The Reflection namespace (System.Reflection) includes classes for interrogating the metadata about assemblies, modules, and types in the CLR.





The MemberInfo classes can be used to execute code at runtime, even if you do not have the project referenced during compilation.





The builder classes can be used to build assemblies, modules, and types at runtime, and they can even allow you to persist new assemblies to disk.





This is the recommended way to create code at runtime!!





Hope this helps.
Reply:why? what is your final goal?
Reply:Not entirely sure where you are going with this, if you are designing an IDE its simple enough. If it is some other type of program the name is set by the programmer and the user would not know or need to know what the object is. You may have a name property on the class which the user can provide.

secret garden

No comments:

Post a Comment