本文整理汇总了C#中System.Type.GetProperties方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetProperties方法的具体用法?C# Type.GetProperties怎么用?C# Type.GetProperties使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetProperties方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
PropertyInfo[] myPropertyInfo;
// Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType("System.Type").GetProperties();
Console.WriteLine("Properties of System.Type are:");
for (int i = 0; i < myPropertyInfo.Length; i++)
{
Console.WriteLine(myPropertyInfo[i].ToString());
}
示例2: Main
//引入命名空间
using System;
using System.Reflection;
// Create a class having six properties.
public class PropertyClass
{
public String Property1
{
get { return "hello"; }
}
public String Property2
{
get { return "hello"; }
}
protected String Property3
{
get { return "hello"; }
}
private Int32 Property4
{
get { return 32; }
}
internal String Property5
{
get { return "value"; }
}
protected internal String Property6
{
get { return "value"; }
}
}
public class Example
{
public static void Main()
{
Type t = typeof(PropertyClass);
// Get the public properties.
PropertyInfo[] propInfos = t.GetProperties(BindingFlags.Public|BindingFlags.Instance);
Console.WriteLine("The number of public properties: {0}.\n",
propInfos.Length);
// Display the public properties.
DisplayPropertyInfo(propInfos);
// Get the nonpublic properties.
PropertyInfo[] propInfos1 = t.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
Console.WriteLine("The number of non-public properties: {0}.\n",
propInfos1.Length);
// Display all the nonpublic properties.
DisplayPropertyInfo(propInfos1);
}
public static void DisplayPropertyInfo(PropertyInfo[] propInfos)
{
// Display information for all properties.
foreach (var propInfo in propInfos) {
bool readable = propInfo.CanRead;
bool writable = propInfo.CanWrite;
Console.WriteLine(" Property name: {0}", propInfo.Name);
Console.WriteLine(" Property type: {0}", propInfo.PropertyType);
Console.WriteLine(" Read-Write: {0}", readable & writable);
if (readable) {
MethodInfo getAccessor = propInfo.GetMethod;
Console.WriteLine(" Visibility: {0}",
GetVisibility(getAccessor));
}
if (writable) {
MethodInfo setAccessor = propInfo.SetMethod;
Console.WriteLine(" Visibility: {0}",
GetVisibility(setAccessor));
}
Console.WriteLine();
}
}
public static String GetVisibility(MethodInfo accessor)
{
if (accessor.IsPublic)
return "Public";
else if (accessor.IsPrivate)
return "Private";
else if (accessor.IsFamily)
return "Protected";
else if (accessor.IsAssembly)
return "Internal/Friend";
else
return "Protected Internal/Friend";
}
}
输出:
The number of public properties: 2. Property name: Property1 Property type: System.String Read-Write: False Visibility: Public Property name: Property2 Property type: System.String Read-Write: False Visibility: Public The number of non-public properties: 4. Property name: Property3 Property type: System.String Read-Write: False Visibility: Protected Property name: Property4 Property type: System.Int32 Read-Write: False Visibility: Private Property name: Property5 Property type: System.String Read-Write: False Visibility: Internal/Friend Property name: Property6 Property type: System.String Read-Write: False Visibility: Protected Internal/Friend
示例3: Type.GetProperties()
//引入命名空间
using System;
using System.Reflection;
public class Test
{
public static void Main(string[] args)
{
TheType.MyClass aClass = new TheType.MyClass();
Type t = aClass.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach(PropertyInfo prop in pi)
Console.WriteLine("Prop: {0}", prop.Name);
}
}
namespace TheType {
public interface IFaceOne {
void MethodA();
}
public interface IFaceTwo {
void MethodB();
}
public class MyClass: IFaceOne, IFaceTwo {
public int myIntField;
public string myStringField;
private double myDoubleField = 0;
public double getMyDouble(){
return myDoubleField;
}
public void myMethod(int p1, string p2)
{
}
public int MyProp
{
get { return myIntField; }
set { myIntField = value; }
}
public void MethodA() {}
public void MethodB() {}
}
}