//
获取变量
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);
}
}
}
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 );
}
}
}
... {
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.
*/
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.
*/