先看这段代码
MainMenu mnu=new MainMenu();
this.Menu=mnu;
mnu.MenuItems.Add("file");
mnu.MenuItems.Add("path");
Object ObjRef=mnu;
Type type=ObjRef.GetType();
if(type.IsClass)
{
MessageBox.Show("A Class");很明显是这个!这里现在是一个MainMenu类型
}
else
{
MessageBox.Show("not a Class");
}
那么type就是一个类了,那我为什么不能
((type)mnu).MenuItems.add();呢
为什么我只能
((MainMenu)mnu).MenuItems.Add()呢
请高手解释一下,为什么不行,我现在想实现这个,有没有好的思路,有代码更好

解决方案 »

  1.   

    我不是你说的高手,但是我知道你的 mnu不是Type类的实例,也就是说: (type)mnu的用法就是错了。 你都知道写Type type = objRef.GetType();了,怎么会犯这种低级的错误? 其次Type也没有MenuItems的属性。 你是否认为是一个类就可以转换呢(哪怕他们是不同的类)?给你一段代码,不知道是不是你想要的:MainMenu menu = ObjRef as MainMenu;
    if( menu !=null)
    {
        menu.MenuItems.Add(...);
    }
      

  2.   

    谢谢你说得那么透彻!但是有一点不明白
    MenuItem和我用objRef.GetType()得到的有什么不同么?
    看看这里
    public static Object[] AddObjCase(DataSet ds,string table,Object ObjRef)
    {

    Object[] ObjAddItem=null;
    try
    {
    ObjAddItem=new Object[ds.Tables[table].Rows.Count];
    for(int i=0;i<ObjAddItem.Length;i++)
    {
    if(ObjRef is MainMenu)
    {
    ObjAddItem[i]=new MenuItem(ds.Tables[table].Rows[i][3].ToString());
    ((MainMenu)ObjRef).MenuItems.Add((MenuItem)ObjAddItem[i]);
    }
    else if(ObjRef is TreeView)
    {
    ObjAddItem[i]=new TreeNode(ds.Tables[table].Rows[i][3].ToString());
    ((TreeView)ObjRef).Nodes.Add((TreeNode)ObjAddItem[i]);
    }
    }
    }
    catch(Exception ErrCase)
    {
    MessageBox.Show(ErrCase.Message);
    }
    return ObjAddItem;
    }
    我是通过传进来的是什么类型的,就对什么类型进行添加
    可是问题很多
    1。不同对象有不同的add方法
    2。如果我不改进写法,那么有n个类型,那我就要写n个判断语句阿,
    我只是想简单化,不管他穿近来什么值,我都动态拿到它的类型,然后实施强制转换,然后我在调用时,自己用add添加!
    比如(写想法,语法错误大家不要说我)
    public static Object[] AddObjCase(DataSet ds,string table,Object ObjRef)
    {
      得到ObjRef这个类型
    然后返回
    }
    这里调用
    MainMenu mnu=this.Menu();
    Object obj=mnu;
    ((AddObjCase)mnu).MenuItems.add()我这里在实际调用时,就知道他是什么东西了,我只要在方法里面把他转换或者得到它的类型就可以了,
    能告诉我怎么实现么?
      

  3.   

    /**
      * A Class that implements the IBase interface
      */
     class ImplementationOne : IBase {
     
      string re = "Re of ImplementationOne";
      static int DELTAFACTOR = 10;
     
      ImplementationOne () {
       Console.WriteLine ("Invoking ImplementationOne...");
      }
      
      public string execute (string data) {
       re = data;
       Console.WriteLine ("ImplementationOne's execute() re is : {0}", re);
       return re;
      }
      
      public double compute (int param1, double param2) {
       Console.WriteLine ("ImplementationOne's compute() method invoked...");
       return param1*param2*DELTAFACTOR;
      }
     }
     
     /**
      * A generic Structure
      */
     struct StructOne {
      int x;
      double y;
      
      double assign () {
       x = 10;
       y = 2.8;
       return x*y;
      }
     }     // If however, it does implement the IBase Interface,
         // create an instance of the object
         object ibaseObject = Activator.CreateInstance (type);
         
         // Create the parameter list
         object[] arguments = new object [] {10, 17.11};
         object result;
         // Dynamically Invoke the Object
         Console.WriteLine ("......Dynamically Invoking compute() on {0} Class", type.FullName);
         result = type.InvokeMember ("compute", 
                                     BindingFlags.Default | BindingFlags.InvokeMethod,
                                     null,
                                     ibaseObject,
                                     arguments);
      

  4.   

    编译期是不会认识Add的,你得手动写,可以去看一下sun的教程,那里reflect讲的比较好,他们都是相通的
      

  5.   

    6楼写了很多,但是我不感觉有多大用处阿!,还有7楼说的,我也是这么沦为的,本来就是手动写,现在就是要让它变成一个MainMenu类型来强制转换他!就针对我的代码,改写一下,行么?小弟刚学这方面的东西,希望大家多帮帮忙,分不够我再开了给!
      

  6.   

    工厂模式
    http://zhenyulu.cnblogs.com/articles/36462.html