您的位置:程序门 -> .net技术 -> c#



关于动态取变量名的值


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


关于动态取变量名的值
发表于:2007-01-04 21:23:50 楼主
我有变量名:
private   string   aa   =   "aa ";
private   string   bb   =   "bb ";

public   string   this[string   test]
{
    get
      {
程序代码
}
}
要求,如果我的test的值是aa(这里要求也上面定义的变量名相符),则返回:aa
如果是bb   则返回bb
发表于:2007-01-04 21:46:231楼 得分:0
class   class1
{
static   void   main(string[]   args)
{
mylist   list   =   new   mylist();
console.writeline(list[ "aa "]);
console.writeline(list[ "bb "]);
}
}

public   class   mylist
{
system.collections.hashtable   hash   =   new   system.collections.hashtable();
public   mylist()
{
hash.add( "aa ", "aa ");
hash.add( "bb ", "bb ");
hash.add( "cc ", "cc ");
}
public   string   this[string   str]  
{  
get   {return   str   +   ": "   +   hash[str].tostring();}
}  

}
发表于:2007-01-04 22:04:142楼 得分:0
谢谢楼上的,我这里用的是web   form  
是放在model里的,所以不想用hashtable
看一下有没有正色好的办法呢
发表于:2007-01-04 22:12:053楼 得分:0
自己搞定了,谢谢大家,

        private   string   heihei   =   "看看能不能 ";

        public   string   heih
        {
                get
                {
                        return   heihei;
                }
        }
        public   string   this[string   ss]
        {
                get
                {
                    return       this.gettype().getfield(ss,   bindingflags.nonpublic   ¦   bindingflags.instance   ¦   bindingflags.public).getvalue(this).tostring();
                }
        }
发表于:2007-01-04 22:13:424楼 得分:0
不知道这样会不会影响效率,这里用到了反射,如果直接用属性,两者的速度将会怎么样呢
        public   string   heih
        {
                get
                {
                        return   heihei;
                }
        }
去完成对外的取值,大家在这里讨论一下吧
发表于:2007-01-04 22:15:455楼 得分:0
用反射性能肯定下降很多,为什么不想用hashtable?
发表于:2007-01-04 22:18:506楼 得分:0
这样吧,怎样?
public   string   this[string   ss]
        {
                get
                {
                        switch(ss){
                        case   "aa "   :
                                return   "aa ";
                        case   "bb "   :
                                return   "bb ";
                        ...
                        }
                }
        }
发表于:2007-01-04 22:25:357楼 得分:0
to:不知道这样会不会影响效率,这里用到了反射,如果直接用属性,两者的速度将会怎么样呢

用反射方法不错,效率上当然会低..

但用反射也比较灵活,如果对效率要求不是很高的话,建议还是用反射..

发表于:2007-01-04 23:26:388楼 得分:0
谢谢,我也是正为这个事发悠呢
发表于:2007-01-04 23:37:009楼 得分:0
在modeinfo层用hashtable好像有点行不通呀
  public   class   cartiteminfo   {

                //   internal   member   variables
                private   int   quantity   =   1;
                private   string   itemid;
                private   string   name;
                private   string   type;
                private   decimal   price;
                private   string   categoryid;
                private   string   productid;

                ///   <summary>
                ///   default   constructor
                ///   </summary>
                public   cartiteminfo()   {   }

                ///   <summary>
                ///   constructor   with   specified   initial   values
                ///   </summary>
                ///   <param   name= "itemid "> id   of   item   to   add   to   cart </param> </param>
                ///   <param   name= "name "> name   of   item </param>
                ///   <param   name= "qty "> quantity   to   purchase </param>
                ///   <param   name= "price "> price   of   item </param>
                ///   <param   name= "type "> item   type </param>    
                ///   <param   name= "categoryid "> parent   category   id </param>
                ///   <param   name= "productid "> parent   product   id </param>
                public   cartiteminfo(string   itemid,   string   name,   int   qty,   decimal   price,   string   type,   string   categoryid,   string   productid)   {
                        this.itemid   =   itemid;
                        this.name   =   name;
                        this.quantity   =   qty;
                        this.price   =   price;
                        this.type   =   type;
                        this.categoryid   =   categoryid;
                        this.productid   =   productid;
                }

                //   properties
                public   int   quantity   {
                        get   {   return   quantity;   }
                        set   {   quantity   =   value;   }
                }

                public   decimal   subtotal   {
                        get   {   return   (decimal)(this.quantity   *   this.price);   }
                }

                public   string   itemid   {
                        get   {   return   itemid;   }
                }

                public   string   name   {
                        get   {   return   name;   }
                }

                public   string   type   {
                        get   {
                                return   type;
                        }
                }
                public   decimal   price   {
                        get   {   return   price;   }
                }

                public   string   categoryid   {
                        get   {
                                return   categoryid;
                        }
                }
                public   string   productid   {
                        get   {
                                return   productid;
                        }
                }
        }
}

我是想把这里的属性换成索引取值的方法,这样不知道会引响多大的速度呀和效率呀,同时如果用索引器的话,是不是还要事先去new一个呢
发表于:2007-01-05 10:19:0610楼 得分:0
1、用hashtable没有什么行不通的
[serializableattribute]  
public   class   cartiteminfo:   hashtable{
        public   cartiteminfo(string   itemid,   string   name,   int   qty,   decimal   price,   string       type,   string   categoryid,   string   productid)   {
          ....
        }
}
2、性能的影响你循环几千次就可以看出区别了。
3、原来的cartiteminfo个人觉得没什么不好,如果每个类需要索引取值的方法你可以加一个方法,而不是全部替换掉(你可以写个基类继承下这样修改起来方便点)。如果不是每个类都需要
我觉得写个静态方法就可以了。
========================
仅供参考


快速检索

最新资讯
热门点击