本文整理汇总了C#中System.Type.GetProperty方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetProperty方法的具体用法?C# Type.GetProperty怎么用?C# Type.GetProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetProperty方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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);
}
}
}
示例2: Main
//引入命名空间
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 Type object of MyClass.
Type myType=typeof(MyClass);
// Get the PropertyInfo by passing the property name and specifying the BindingFlags.
PropertyInfo myPropInfo = myType.GetProperty("MyProperty", BindingFlags.Public | BindingFlags.Instance);
// Display Name propety to console.
Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name);
}
catch(NullReferenceException e)
{
Console.WriteLine("MyProperty does not exist in MyClass." +e.Message);
}
}
}
示例3: Main
//引入命名空间
using System;
using System.Reflection;
class MyClass1
{
String myMessage="Hello World.";
public string MyProperty1
{
get
{
return myMessage;
}
set
{
myMessage =value;
}
}
}
class TestClass
{
static void Main()
{
try
{
Type myType = typeof(MyClass1);
// Get the PropertyInfo object representing MyProperty1.
PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1",
typeof(string));
Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name);
Console.WriteLine("The type of the first property of MyClass1 is {0}.", myStringProperties1.PropertyType);
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException :"+e.Message);
}
catch(AmbiguousMatchException e)
{
Console.WriteLine("AmbiguousMatchException :"+e.Message);
}
catch(NullReferenceException e)
{
Console.WriteLine("Source : {0}" , e.Source);
Console.WriteLine("Message : {0}" , e.Message);
}
//Output:
//The name of the first property of MyClass1 is MyProperty1.
//The type of the first property of MyClass1 is System.String.
}
}
示例4: Main
//引入命名空间
using System;
using System.Reflection;
class MyClass1
{
private int [,] myArray = {{1,2},{3,4}};
// Declare an indexer.
public int this [int i,int j]
{
get
{
return myArray[i,j];
}
set
{
myArray[i,j] = value;
}
}
}
public class MyTypeClass
{
public static void Main(string[] args)
{
try
{
// Get the Type object.
Type myType=typeof(MyClass1);
Type[] myTypeArr = new Type[2];
// Create an instance of a Type array.
myTypeArr.SetValue(typeof(int),0);
myTypeArr.SetValue(typeof(int),1);
// Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);
// Display the property.
Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString());
}
catch(NullReferenceException e)
{
Console.WriteLine("An exception occurred.");
Console.WriteLine("Source : {0}" , e.Source);
Console.WriteLine("Message : {0}" , e.Message);
}
}
}
示例5: Main
//引入命名空间
using System;
using System.Reflection;
public class MyPropertyClass
{
private int [,] myPropertyArray = new int[10,10];
// Declare an indexer.
public int this [int i,int j]
{
get
{
return myPropertyArray[i,j];
}
set
{
myPropertyArray[i,j] = value;
}
}
}
public class MyTypeClass
{
public static void Main()
{
try
{
Type myType=typeof(MyPropertyClass);
Type[] myTypeArray = new Type[2];
// Create an instance of the Type array representing the number, order
// and type of the parameters for the property.
myTypeArray.SetValue(typeof(int),0);
myTypeArray.SetValue(typeof(int),1);
// Search for the indexed property whose parameters match the
// specified argument types and modifiers.
PropertyInfo myPropertyInfo = myType.GetProperty("Item",
typeof(int),myTypeArray,null);
Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name +
" has a property type of " + myPropertyInfo.PropertyType);
}
catch(Exception ex)
{
Console.WriteLine("An exception occurred " + ex.Message);
}
}
}