set class name and object name at run time
How do I specify the object name and class name that is to be created at runtime ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Nov 18, 2013, 10:48 AM
I can't tell from the code exactly what this variable represents but the most likely reasons for the error are:
1. That it's not a 'fully qualified' typename i.e. projectName doesn't represent a namespace or FormName doesn't represent a type.
2. There's a difference in capitalization between the actual type and the variable.
Mehrdad AbdolahiPosted Nov 16, 2013, 3:42 AM
I could not use from that code
please help me
Mehrdad AbdolahiPosted Nov 16, 2013, 3:40 AM
private void btnCustomers_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn = (Button)sender;
MessageBox.Show(btn.Name);
StringBuilder EntityName = new StringBuilder();
EntityName.Append(btn.Name);
EntityName.Remove(0, 3);
string projectName = "AAShS_Forms_" + EntityName.ToString();
StringBuilder FormName = new StringBuilder();
FormName.Append("F_C_");
FormName.Append(EntityName.ToString());
String NameOfSpace = projectName + "." + FormName.ToString();
object ObjForm = new object();
** Type TypeForm= System.Reflection.Assembly.GetExecutingAssembly().GetType(NameOfSpace );
if (TypeForm != null){
ObjForm = Activator.CreateInstance(TypeForm);
((Form)ObjForm).ShowDialog();
}
}
.........
but I don't know why answer is Null in (**line) (TypeForm)?!
VulpesPosted Nov 10, 2013, 3:39 PM
However, this is quite complicated and requires a knowledge of MSIL opcodes so it's not something which is done very often in practice. Check out this article for details:
http://www.c-sharpcorner.com/uploadfile/puranindia/reflection-and-reflection-emit-in-C-Sharp/