When using reflection,how to get the field without creating a instance?
-
Hi everyone,now I have a winform Form1,in Form1,there is a TextBox1,and its Text is "abcd". In the other project,I want to get the Text of TextBox1,as below: Assembly assembly = Assembly.LoadFrom(@"ClassLibrary1.dll"); Type[] types = assembly.GetTypes(); FieldInfo[] fis; System.Windows.Forms.TextBox tb; for (int i = 0; i < types.Length; i++) { fis = types[i].GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int j = 0; j < fis.Length; j++) { if (fis[j].FieldType == typeof(System.Windows.Forms.TextBox)) { tb = fis[j].GetValue(Activator.CreateInstance(types[i]))as System.Windows.Forms.TextBox; Console.WriteLine(tb.Text); } } } Console.Read(); But I have to create a instance "Activator.CreateInstance(types[i]))".Are there any ways that I can get the Text without create a Form1 instance? Anyone who can help it would be appreciated.
-
Hi everyone,now I have a winform Form1,in Form1,there is a TextBox1,and its Text is "abcd". In the other project,I want to get the Text of TextBox1,as below: Assembly assembly = Assembly.LoadFrom(@"ClassLibrary1.dll"); Type[] types = assembly.GetTypes(); FieldInfo[] fis; System.Windows.Forms.TextBox tb; for (int i = 0; i < types.Length; i++) { fis = types[i].GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int j = 0; j < fis.Length; j++) { if (fis[j].FieldType == typeof(System.Windows.Forms.TextBox)) { tb = fis[j].GetValue(Activator.CreateInstance(types[i]))as System.Windows.Forms.TextBox; Console.WriteLine(tb.Text); } } } Console.Read(); But I have to create a instance "Activator.CreateInstance(types[i]))".Are there any ways that I can get the Text without create a Form1 instance? Anyone who can help it would be appreciated.
Morven Huang wrote:
Are there any ways that I can get the Text without create a Form1 instance?
Since the textbox is a member of Form1, you need to instantiate Form1 for getting access to text box.
Navaneeth How to use google | Ask smart questions
-
Hi everyone,now I have a winform Form1,in Form1,there is a TextBox1,and its Text is "abcd". In the other project,I want to get the Text of TextBox1,as below: Assembly assembly = Assembly.LoadFrom(@"ClassLibrary1.dll"); Type[] types = assembly.GetTypes(); FieldInfo[] fis; System.Windows.Forms.TextBox tb; for (int i = 0; i < types.Length; i++) { fis = types[i].GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int j = 0; j < fis.Length; j++) { if (fis[j].FieldType == typeof(System.Windows.Forms.TextBox)) { tb = fis[j].GetValue(Activator.CreateInstance(types[i]))as System.Windows.Forms.TextBox; Console.WriteLine(tb.Text); } } } Console.Read(); But I have to create a instance "Activator.CreateInstance(types[i]))".Are there any ways that I can get the Text without create a Form1 instance? Anyone who can help it would be appreciated.
That would be like asking for the colour of a car that doesn't exist. You know that cars have colours - but you the colour doesn't exist without the car.
Mark Churchill Director, Dunn & Churchill Pty Ltd Free Download: Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.
Alpha release: Entanglar: Transparant multiplayer framework for .Net games. -
Hi everyone,now I have a winform Form1,in Form1,there is a TextBox1,and its Text is "abcd". In the other project,I want to get the Text of TextBox1,as below: Assembly assembly = Assembly.LoadFrom(@"ClassLibrary1.dll"); Type[] types = assembly.GetTypes(); FieldInfo[] fis; System.Windows.Forms.TextBox tb; for (int i = 0; i < types.Length; i++) { fis = types[i].GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); for (int j = 0; j < fis.Length; j++) { if (fis[j].FieldType == typeof(System.Windows.Forms.TextBox)) { tb = fis[j].GetValue(Activator.CreateInstance(types[i]))as System.Windows.Forms.TextBox; Console.WriteLine(tb.Text); } } } Console.Read(); But I have to create a instance "Activator.CreateInstance(types[i]))".Are there any ways that I can get the Text without create a Form1 instance? Anyone who can help it would be appreciated.
Thanks everybody.