Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. When using reflection,how to get the field without creating a instance?

When using reflection,how to get the field without creating a instance?

Scheduled Pinned Locked Moved C#
helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Morven Huang
    wrote on last edited by
    #1

    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.

    N M M 3 Replies Last reply
    0
    • M Morven Huang

      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.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • M Morven Huang

        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.

        M Offline
        M Offline
        Mark Churchill
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • M Morven Huang

          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.

          M Offline
          M Offline
          Morven Huang
          wrote on last edited by
          #4

          Thanks everybody.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups