当前位置:编程学习 > C#/ASP.NET >>

代码有问题!!

有些问题啊 ,这样是属性访问 
但是现在有些问题 

如何写啊? 

string objName = chkItem.Name.Substring(3).Trim().ToString();  

                    if (chkItem.Checked)  
                    {  
                        FieldsObject["objName"] = true;  
                    }  
                    else  
                    {  
                        FieldsObject["objName"] = false;  
                    }  
关键是属性是动态的怎么处理啊? 
example:  obj.xxx = ""; 
xxx是动态的啊 --------------------编程问答-------------------- 这个我等等帮你弄,放水先 --------------------编程问答--------------------

string objName = chkItem.Name.Substring(3).Trim().ToString();   

                    if (chkItem.Checked)   
                    {   
                        FieldsObject[objName ] = true;   
                    }   
                    else   
                    {   
                        FieldsObject[objName ] = false;   
                    }   
//这个可以直接这样子写


--------------------编程问答-------------------- 如果属性定义的不是索引。你可以这样来

 string objName = "Test";//chkItem.Name.Substring(3).Trim().ToString();
            //System.Reflection.Assembly ass = System.Reflection.Assembly.Load(@"*.dll");
            Type t = Type.GetType("ConsoleApplication1.Class1");            
            //无参数构造情况
            Class1 obj = (Class1)System.Activator.CreateInstance(t);          
            System.Reflection.PropertyInfo[] pis = t.GetProperties();
            System.Reflection.PropertyInfo pi = null;
            for (int i = 0; i < pis.Length; i++)
            {
                if (pis[i].Name ==objName)
                {
                    pi = pis[i];
                    break;
                }
            }          
            if (chkItem.Checked)
            {
                pi.SetValue(obj, true, null);
            }
            else
            {
                pi.SetValue(obj, false, null);
            }   
--------------------编程问答-------------------- Class1类是这样的

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{   
    public class Class1
    {
        public Class1()
        {
 
        }
       private bool flag;

       public bool this[string name]
       {
           get 
           { 
               return this.flag;
           }
           set 
           {
               this.flag = value;
           }
       }

        private bool test;
        public bool Test
        {
            get { return this.test; }
            set { this.test = value; }
        } 
    }
}

--------------------编程问答--------------------

jiefen

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,