SetValue using reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • samlee

    #1

    SetValue using reflection

    Hi, all
    I must apologise if these questions had been asked before.

    1)
    Why Assembly.Load(" System.Windows. Forms") ?

    2)
    How can I set/get property of an object using reflection.
    More specfically, how to acomplish the effect of
    this.Text = "Hello World";
    in the following program

    Thank in advance,
    =============== =============== =============== ===
    using System;
    using System.Reflecti on;
    using System.IO;

    public class Form1
    {
    static object sf;
    public Form1()
    {
    //this.Text = "Hello World";
    Assembly a1 =
    Assembly.LoadFr om(@"C:\WINDOWS \Microsoft.NET\ Framework\v1.1. 4322\System.Win dows.Forms.dll" );
    Type t = a1.GetType("Sys tem.Windows.For ms.Form");
    sf= Activator.Creat eInstance (t);
    // Type t1 = a1.GetType("Sys tem.Windows.For ms.Control");
    // PropertyInfo prop= t1.GetProperty( "Text", new
    Type[]{typeof(string) }); // kein Parameter
    // object[] args = new object[]{"hello world" };
    // object result = prop.SetValue(t , args,null); // kein Parameter
    }

    static void Main()
    {
    //System.Windows. Forms.Applicati on.Run(new Form1());
    Form1 s= new Form1();
    Assembly a1 =
    Assembly.LoadFr om(@"C:\WINDOWS \Microsoft.NET\ Framework\v1.1. 4322\System.Win dows.Forms.dll" );
    // Assembly a1 = Assembly.Load(" System.Windows. Forms");
    Type t1 = a1.GetType("Sys tem.Windows.For ms.Form");
    Type t2 = a1.GetType("Sys tem.Windows.For ms.Application" );
    MethodInfo method = t2.GetMethod("R un", new Type[]{t1});
    object result = method.Invoke(n ull, new object[]{sf});
    }
    }

  • Mattias Sjögren

    #2
    Re: SetValue using reflection

    >1)[color=blue]
    >Why Assembly.Load(" System.Windows. Forms") ?[/color]

    Why it fails? You should specify the full assembly name

    "System.Windows .Forms, Version=1.0.500 0.0, Culture=neutral ,
    PublicKeyToken= b77a5c561934e08 9"

    [color=blue]
    >2)
    >How can I set/get property of an object using reflection.
    >More specfically, how to acomplish the effect of
    >this.Text = "Hello World";
    >in the following program[/color]

    t1.GetProperty( "Text", typeof(string)) .SetValue(sf, "Hello World",
    null);



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • samlee

      #3
      Re: SetValue using reflection

      > t1.GetProperty( "Text", typeof(string)) .SetValue(sf, "Hello World",[color=blue]
      > null);[/color]

      Thank you very much, It works.

      A related question. How to define type Form1 as a derived type from
      System.Windows. Forms.Form and its overload methods and ctor?
      Because I wish to implement the following using reflection.

      public class Form1:System.Wi ndows.Forms.For m
      {
      public Form1() {this.Text = "Hello World";}
      }

      Comment

      Working...