C# 反射

//获取变量
using System;
using System.Reflection;

class MyClass
{
    
private int myProperty;
    
// Declare MyProperty.
    public int MyProperty
    
{
        
get
        
{
            
return myProperty;
        }

        
set
        
{
            myProperty
=value;
        }

    }

}

public class MyTypeClass
{
    
public static void Main(string[] args)
    
{
        
try
        
{
            
// Get the Type object corresponding to MyClass.
            Type myType=typeof(MyClass);       
            
// Get the PropertyInfo object by passing the property name.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
            
// Display the property name.
            Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
        }

        
catch(NullReferenceException e)
        
{
            Console.WriteLine(
"The property does not exist in MyClass." + e.Message);
        }

    }

}

 //获取所有变量

 

class MyClass
{
   
public int myInt = 0;
   
public string myString = null;

   
public MyClass()
   
{
   }

   
public void Myfunction()
   
{
   }

}


class Type_GetMembers
{
   
public static void Main()
   
{
      
try
      
{
         MyClass myObject 
= new MyClass();
         MemberInfo[] myMemberInfo; 

         
// Get the type of 'MyClass'.
         Type myType = myObject.GetType(); 
        
         
// Get the information related to all public member's of 'MyClass'. 
         myMemberInfo = myType.GetMembers();
    
         Console.WriteLine( 
" The members of class '{0}' are : ", myType); 
         
for (int i =0 ; i < myMemberInfo.Length ; i++)
         
{
            
// Display name and type of the concerned member.
            Console.WriteLine( "'{0}' is a {1}", myMemberInfo[i].Name, myMemberInfo[i].MemberType);
         }

      }

      
catch(SecurityException e)
      
{
         Console.WriteLine(
"Exception : " + e.Message ); 
      }

   }

}

c#用反射获取类型,然后动态的生成变量
   现在我们要获取 ProjectDataDetail myDataDetail 下面的一个属性 D01_01
   即:projectDataDetail.D01_01的值,我们需要在循环中动态建造D01_01属性

1、获取单个的属性:
  Type myType = myDataDetail.GetType();
  System.Reflection.PropertyInfo myPI = myType.GetProperty("D01_01") ;
  string myValue = myPI.GetValue(myDataDetail,null).ToString() ;

2、可以获取属性组:
                System.Reflection.PropertyInfo [] myPIs = myType.GetProperties () ;

 

using System;
using System.Reflection;
 
// Define a class with a property.
public class TestClass   
{
    
private string caption = "A Default caption";
    
public string Caption
    
{
        
get return caption; }
        
set 
        

            
if (caption != value) 
            
{
                caption 
= value;
            }

        }

    }

}

 
class TestPropertyInfo
{
    
public static void Main()
    
{
        TestClass t 
= new TestClass();
 
        
// Get the type and PropertyInfo.
        Type myType = t.GetType();
        PropertyInfo pinfo 
= myType.GetProperty("Caption");
 
        
// Display the property value, using the GetValue method.
        Console.WriteLine(" GetValue: " + pinfo.GetValue(t, null));
 
        
// Use the SetValue method to change the caption.
        pinfo.SetValue(t, "This caption has been changed."null);
 
        
//  Display the caption again.
        Console.WriteLine("GetValue: " + pinfo.GetValue(t, null));

        Console.WriteLine(
" Press the Enter key to continue.");
        Console.ReadLine();
    }

}


/*
This example produces the following output:
 
GetValue: A Default caption
GetValue: This caption has been changed

Press the Enter key to continue.
*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值