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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 10, 2013, 10:39 AM EST
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/
Mehrdad Abdolahi9 reputationPosted Nov 15, 2013, 10:40 PM EST
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)?!
Mehrdad Abdolahi9 reputationPosted Nov 15, 2013, 10:42 PM EST
I could not use from that code
please help me
VulpesRank #55 · 29.2K reputationPosted Nov 18, 2013, 5:48 AM EST
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.