msdn里面的小例子,谁能帮解释一下 C#(50分)

  • 主题发起人 主题发起人 wdl
  • 开始时间 开始时间
W

wdl

Unregistered / Unconfirmed
GUEST, unregistred user!
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);
}
}
}
不好意思,没看懂啊[:(]
Type myType=typeof(MyPropertyClass);
Type[] myTypeArray = new Type[2];
myTypeArray和myType什么关系?
 
这么长的代码< 分又这么少 。 另找高人吧。[:)][:D]
 
可以说没有什么关系
myTypeArray数组中存储的是Type
有关Type的介绍可以看MSDN中Type Members
 
其实我是想知道GetProperty是干什么用的,找到了这个示例。
谁能帮我理解一下?[:)]
 
public PropertyInfo GetProperty(
string name,
Type returnType,
Type[] types,
ParameterModifier[] modifiers
);

name
The String containing the name of the public property to get.
returnType
The return type of the property.
types
An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.
-or-
An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.
modifiers
An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. The default binderdo
es not process this parameter.
 
接受答案了.
 
后退
顶部