none
Assembly.Load, PropertyGrid and generic class related problem RRS feed

  • Question

  • OK, this is a complicated case (at least for me). Let me try to describe it as clearly as I can -
     
    I have an assembly contains a Form (Form1) with a PropertyGrid and associate its SelectedObject to a class:
     
    propertyGrid1.SelectedObject = new MyWrapper();
     
      public class MyWrapper
      {
        Class3 m_class3 = new Class3();
        public MyWrapper()
        {
        }
        [Category("Category1"), DisplayName("DisplayName1")]
        [TypeConverter(typeof(MyConverter<int>))]
        public Class3 Property1
        {
          get
          {
            return m_class3;
          }
          set
          {
            m_class3 = value;
          }
        }
      }
    

     
    Property1 has a TypeConverterAttribute -
    <span>  public class MyConverter<T> : TypeConverter
      {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            //it is null when Assembly.Load from byte array
            if (ABC.abc == null)
              System.Windows.Forms.MessageBox.Show("abc is null");
            else
              System.Windows.Forms.MessageBox.Show("abc is not null");
        }
      }</span>
    
    

     
    ABC is a static class hold a static member variable -
      public static class ABC
      {
        public static Class2 abc;
      }
     
    

    ABC.abc is initialized in another class -
    <span>  public class Class1
      {
        public void method1()
        {
          ABC.abc = new Class2();
          Form1 form = new Form1();
          form.ShowDialog();
        }
      }</span>
    
    

     
    I had another console application which will load above assembly and create a Class1 instance and invoke method1. Now the problem comes - if I read the assembly to byte array and load it by Assembly.Load(byte[]), ABC.abc is always null. But if I load it by Assembly.LoadFrom, everything is OK. Is it strange? And I found if I change the generic class MyConverter<T> to a simple class MyConverter. It works well for both cases.
    assem = Assembly.Load(assemStream);
    Type t = assem.GetType("Class1");
    object addin = System.Activator.CreateInstance(t);
    Type rt = addin.GetType();
    Type[] emptyParam = new Type[0];
    // get the method
    System.Reflection.MethodInfo macroMethod = rt.GetMethod(
              "method1",
              BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
              null,
              emptyParam,
              null);
    macroMethod.Invoke(addin, null);
     
    

    I uploaded the entire source code here . Hope it can help you to understand my question clearly.
     
    Please help.
    Thursday, March 17, 2011 7:08 AM

All replies