📄 sample25.cs
字号:
namespace apiBook
{
using System;
using System.Reflection;
using System.Text;
using System.Security;
public class TestClass
{
public int number=123456;
public string testfield = "test Field";
public string testFieldStr
{
get
{
return testfield;
}
set
{
if(testfield!=value)
{
testfield=value;
}
}
}
public int testFieldInt
{
get
{
return number;
}
set
{
if(number!=value)
{
number=value;
}
}
}
public int testMethod(int i)
{
return i;
}
}
public class TestTypeClass
{
public static void Main()
{
// 使用本地机器
string testServer="localhost";
// 使用的程序ID为DispatchMapper.DispatchMapper.1
string s ="DispatchMapper.DispatchMapper.1";
Type testType =Type.GetTypeFromProgID(s,testServer,true);
//使用GetTypeFromProgID方法获取GUID
Console.WriteLine("程序ID为DispatchMapper.DispatchMapper.1的GUID是:");
Guid guid=new Guid ("{000209FF-0000-0000-C000-000000000046}");
Console.WriteLine(testType.GUID);
testType=Type.GetTypeFromCLSID(guid);
//使用GetTypeFromCLSID方法获取该对象类型信息
Console.WriteLine("testType.GUID="+testType.GUID);
Console.WriteLine("testType.ToString()="+testType.ToString());
Console.WriteLine();
TestClass testClass=new TestClass();
testType=typeof(TestClass);
FieldInfo testFieldInfo=testType.GetField("testfield");
//使用GetField方法获取类型的Field信息
Console.WriteLine("TestClass类中testfield域的值为:"+testFieldInfo.GetValue(testClass));
FieldInfo[] testFields=testType.GetFields();
//使用GetFields方法
for(int i=0;i<testFields.Length;i++)
{
Console.WriteLine("TestClass类中域"+testFields[i].Name+"的值是:"+testFields[i].GetValue(testClass));
//使用GetValue方法获取对象中对应域的值
}
MemberInfo testMemberInfo = testType.GetMethod("testMethod", BindingFlags.Public |
BindingFlags.Instance, null,CallingConventions.Any,
new Type[] {typeof(int)}, null);
Console.WriteLine("testMethod方法的信息:");
Console.WriteLine(testMemberInfo);
MemberInfo[] testMember=testType.GetMember("test*");
Console.WriteLine("以test开头的成员信息:");
for(int i=0;i<testMember.Length;i++)
{
Console.WriteLine(testMember[i]);
}
PropertyInfo[] testPropertyInfo = testType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine();
Console.WriteLine("类中的公共属性的个数是:"+testPropertyInfo.Length);
Console.WriteLine();
Console.WriteLine("类中公共属性的名称是:");
for(int i=0;i<testPropertyInfo.Length;i++)
{
Console.WriteLine("\n属性名::"+testPropertyInfo[i].Name);
Console.WriteLine("属性的类型:"+testPropertyInfo[i].PropertyType);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -