属性是这样的成员:它们提供灵活的机制来读取、编写或计算私有字段的值。可以像使用公共数据成员一样使用属性,但实际上它们是称作“访问器”的特殊方法。这使得可以轻松访问数据,此外还有助于提高方法的安全性和灵活性。 在本示例中,TimePeriod 类存储一个时间段。在内部,类以秒为单位存储时间,但客户端使用名为 Hours 的属性能够以小时为单位指定时间。Hours 属性的访问器执行小时和秒之间的转换。 class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } } class Program { static void Main() { TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called. t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called. System.Console.WriteLine("Time in hours: " + t.Hours); } } // Output: Time in hours: 24 http://msdn.microsoft.com/zh-cn/library/x9fsa0sw.aspx 反射(C# 编程指南) 反射提供了封装程序集、模块和类型的对象(Type 类型)。可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。 反射在下列情况下很有用: 当需要访问程序元数据中的属性时。请参见主题使用反射访问属性。 检查和实例化程序集中的类型。 在运行时构建新类型。使用 System.Reflection.Emit 中的类。 执行后期绑定,访问在运行时创建的类型的方法。请参见主题动态加载和使用类型。 http://msdn.microsoft.com/zh-cn/library/ms173183.aspx 那个吗····using System; using System.Reflection;// An enumeration of animals. Start at 1 (0 = uninitialized). public enum Animal { // Pets. Dog = 1, Cat, Bird, }// A custom attribute to allow a target to have a pet. public class AnimalTypeAttribute : Attribute { // The constructor is called when the attribute is set. public AnimalTypeAttribute(Animal pet) { thePet = pet; } // Keep a variable internally ... protected Animal thePet; // .. and show a copy to the outside world. public Animal Pet { get { return thePet; } set { thePet = Pet; } } }// A test class where each method has its own pet. class AnimalTypeTestClass { [AnimalType(Animal.Dog)] public void DogMethod() { } [AnimalType(Animal.Cat)] public void CatMethod() { } [AnimalType(Animal.Bird)] public void BirdMethod() { } }class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { AnimalTypeTestClass testClass = new AnimalTypeTestClass(); Type type = testClass.GetType(); // Iterate through all the methods of the class. foreach (MethodInfo mInfo in type.GetMethods()) { // Iterate through all the Attributes for each method. foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo)) { // Check for the AnimalType attribute. if (attr.GetType() == typeof(AnimalTypeAttribute)) outputBlock.Text += String.Format( "Method {0} has a pet {1} attribute.", mInfo.Name, ((AnimalTypeAttribute)attr).Pet) + "\n"; } } } } /* * Output: * Method DogMethod has a pet Dog attribute. * Method CatMethod has a pet Cat attribute. * Method BirdMethod has a pet Bird attribute. */ Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。所有属性类型都直接或间接地从 Attribute 类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用 AttributeTargets 类可以指定属性所应用到的目标元素。Attribute 类提供检索和测试自定义属性的简便方法。
索引器允许按照数组的额方式索引对象的数组元素,索引器可以使用户像访问数组一样访问类成员,索引器类似于属性,不同之处在于它们的访问器采用参数。 例: class IndexerClass { //定义数组 private int[] arr = new int[100]; //定义索引器 public int this[int index] // Indexer declaration { get { // 检查索引值. if (index < 0 || index >= 100) { return 0; } else { return arr[index]; } } set { if (!(index < 0 || index >= 100)) { arr[index] = value; } } } } 定义好索引,就可以在程序主函数中使用了 class MainClass { static void Main() { IndexerClass test = new IndexerClass(); // 利用索引器直接初始化对象. test[3] = 256; test[5] = 1024; for (int i = 0; i <= 10; i++) { System.Console.WriteLine("Element #{0} = {1}", i, test[i]); } } }
属性(C# 编程指南)属性是这样的成员:它们提供灵活的机制来读取、编写或计算私有字段的值。可以像使用公共数据成员一 样使用属性,但实际上它们是称为“访问器”的特殊方法。这使得数据在可被轻松访问的同时,仍能提供方 法的安全性和灵活性。在本示例中,类 TimePeriod 存储了一个时间段。类内部以秒为单位存储时间,但提供一个称为 Hours 的属性,它允许客户端指定以小时为单位的时间。Hours 属性的访问器执行小时和秒之间的转换。 class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } }class Program { static void Main() { TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called. t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called. System.Console.WriteLine("Time in hours: " + t.Hours); } } 索引器(C# 编程指南)索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于 属性,不同之处在于它们的访 问器采用参数。在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索值的 方法)。Program 类为存储字符串创建了此类的一个实例。class SampleCollection<T> { private T[] arr = new T[100]; public T this[int i] { get { return arr[i]; } set { arr[i] = value; } } }// This class shows how client code uses the indexer class Program { static void Main(string[] args) { SampleCollection<string> stringCollection = new SampleCollection<string>(); stringCollection[0] = "Hello, World"; System.Console.WriteLine(stringCollection[0]); } }参考http://msdn.microsoft.com/zh-cn/library/6x16t2tx(VS.80).aspx事件,参考 http://msdn.microsoft.com/zh-cn/library/awbftdfh(VS.80).aspx
索引器使得对象可按照与数组相似的方法进行索引。 get 访问器返回值。set 访问器分配值。 this 关键字用于定义索引器。 value 关键字用于定义由 set 索引器分配的值。 索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。 索引器可被重载。 索引器可以有多个形参,class Sample { string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" }; private int GetDay(string testDay) { int i = 0; foreach (string day in days) { if (day == testDay) { return i; } i++; } return -1; } public int this[string day] { get { return (GetDay(day)); } } } 属性其实就是特殊的类成员,它实现了对私有类域的受控访问。在C#语言中有两种属性方法,其一是get,通过它可以返回私有域的值,其二是set,通过它就可以设置私有域的值。public class Student { private string name; public string Name { get { return name; } set { name = value; } } } class Program { static void Main(string[] args) { Student student = new Student(); student.Name = "A"; Console.WriteLine(student.Name); Console.Read(); } } 类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。 属性可以是static(静态的)而索引器则必须是实例成员。看看msdn
class TimePeriod
{
private double seconds; public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}
class Program
{
static void Main()
{
TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called.
t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called.
System.Console.WriteLine("Time in hours: " + t.Hours);
}
}
// Output: Time in hours: 24 http://msdn.microsoft.com/zh-cn/library/x9fsa0sw.aspx 反射(C# 编程指南) 反射提供了封装程序集、模块和类型的对象(Type 类型)。可以使用反射动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型并调用其方法或访问其字段和属性。如果代码中使用了属性,可以利用反射对它们进行访问。
反射在下列情况下很有用: 当需要访问程序元数据中的属性时。请参见主题使用反射访问属性。 检查和实例化程序集中的类型。 在运行时构建新类型。使用 System.Reflection.Emit 中的类。 执行后期绑定,访问在运行时创建的类型的方法。请参见主题动态加载和使用类型。
http://msdn.microsoft.com/zh-cn/library/ms173183.aspx
那个吗····using System;
using System.Reflection;// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal
{
// Pets.
Dog = 1,
Cat,
Bird,
}// A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute
{
// The constructor is called when the attribute is set.
public AnimalTypeAttribute(Animal pet)
{
thePet = pet;
} // Keep a variable internally ...
protected Animal thePet; // .. and show a copy to the outside world.
public Animal Pet
{
get { return thePet; }
set { thePet = Pet; }
}
}// A test class where each method has its own pet.
class AnimalTypeTestClass
{
[AnimalType(Animal.Dog)]
public void DogMethod() { } [AnimalType(Animal.Cat)]
public void CatMethod() { } [AnimalType(Animal.Bird)]
public void BirdMethod() { }
}class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
AnimalTypeTestClass testClass = new AnimalTypeTestClass();
Type type = testClass.GetType();
// Iterate through all the methods of the class.
foreach (MethodInfo mInfo in type.GetMethods())
{
// Iterate through all the Attributes for each method.
foreach (Attribute attr in
Attribute.GetCustomAttributes(mInfo))
{
// Check for the AnimalType attribute.
if (attr.GetType() == typeof(AnimalTypeAttribute))
outputBlock.Text += String.Format(
"Method {0} has a pet {1} attribute.",
mInfo.Name, ((AnimalTypeAttribute)attr).Pet) + "\n";
} }
}
}
/*
* Output:
* Method DogMethod has a pet Dog attribute.
* Method CatMethod has a pet Cat attribute.
* Method BirdMethod has a pet Bird attribute.
*/
Attribute 类将预定义的系统信息或用户定义的自定义信息与目标元素相关联。目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性 (Property)、返回值、结构或其他属性 (Attribute)。属性所提供的信息也称为元数据。元数据可由应用程序在运行时进行检查以控制程序处理数据的方式,也可以由外部工具在运行前检查以控制应用程序处理或维护自身的方式。例如,.NET Framework 预定义属性类型并使用属性类型控制运行时行为,某些编程语言使用属性类型表示 .NET Framework 公共类型系统不直接支持的语言功能。所有属性类型都直接或间接地从 Attribute 类派生。属性可应用于任何目标元素;多个属性可应用于同一目标元素;并且属性可由从目标元素派生的元素继承。使用 AttributeTargets 类可以指定属性所应用到的目标元素。Attribute 类提供检索和测试自定义属性的简便方法。
例:
class IndexerClass
{
//定义数组
private int[] arr = new int[100];
//定义索引器
public int this[int index] // Indexer declaration
{
get
{
// 检查索引值.
if (index < 0 || index >= 100)
{
return 0;
}
else
{
return arr[index];
}
}
set
{
if (!(index < 0 || index >= 100))
{
arr[index] = value;
}
}
}
}
定义好索引,就可以在程序主函数中使用了
class MainClass
{
static void Main()
{
IndexerClass test = new IndexerClass();
// 利用索引器直接初始化对象.
test[3] = 256;
test[5] = 1024;
for (int i = 0; i <= 10; i++)
{
System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
}
}
}
图1 事件驱动的基本模型
事件是一种特殊的委托特化实例,而这种委托特化的签名必须遵从特定的格式,如public delegate void EventHandler(object source ,MyEventArgs e) ;其中,source表示事件的发生源,一般是某个控件或窗体;MyEventArgs这个类必须从EventArgs类继承,该类用于封装与事件相关的信息。Provider类可按如下代码声明事件:public event EventHandler OkClick ;//发布事件这也就是发布事件。既然事件是一个委托特化实例,那么它也相当于一个函数指针队列,初始时这个队列是空的。另外我们要在Master类中定义事件处理函数,以便在事件发生时调用该处理函数。private void Process_OkClick (object source ,MyEventArgs e) {......}注意,该函数的签名必须与上面的EventHandler的签名完全一致。接下来,我们可以向OkClick这个队列中添加函数指针。像这样:OkClick += new EventHandler(Process_OkClick) ;//预定事件,注册这就是在Master中预定事件。发布事件的类Provider可以在适当的时候触发事件,像这样this.OkClick(); 要正确理解“触发事件”的意思。“触发事件”并不是“使事件发生”,而是“当事件出现(发生)时,使事件处理函数执行(即调用事件处理函数)”。上面说“发布事件的类Provider可以在适当的时候触发事件”,这个“适当的时候”就是我们指定的事件发生的时候。比如,系统本身就可以有很多事件发生,如鼠标左键按下,这表示事件发生,此时系统就触发相应的事件,使对应的事件处理函数执行。触发事件的本质是让程序依次跳转到函数指针队列中的指针指向的地址,去执行相应的函数。本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhuweisky/archive/2005/07/06/415669.aspx
样使用属性,但实际上它们是称为“访问器”的特殊方法。这使得数据在可被轻松访问的同时,仍能提供方
法的安全性和灵活性。在本示例中,类 TimePeriod 存储了一个时间段。类内部以秒为单位存储时间,但提供一个称为 Hours
的属性,它允许客户端指定以小时为单位的时间。Hours 属性的访问器执行小时和秒之间的转换。
class TimePeriod
{
private double seconds; public double Hours
{
get { return seconds / 3600; }
set { seconds = value * 3600; }
}
}class Program
{
static void Main()
{
TimePeriod t = new TimePeriod(); // Assigning the Hours property causes the 'set' accessor to be called.
t.Hours = 24; // Evaluating the Hours property causes the 'get' accessor to be called.
System.Console.WriteLine("Time in hours: " + t.Hours);
}
}
索引器(C# 编程指南)索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于 属性,不同之处在于它们的访
问器采用参数。在下面的示例中,定义了一个泛型类,并为其提供了简单的 get 和 set 访问器方法(作为分配和检索值的
方法)。Program 类为存储字符串创建了此类的一个实例。class SampleCollection<T>
{
private T[] arr = new T[100];
public T this[int i]
{
get
{
return arr[i];
}
set
{
arr[i] = value;
}
}
}// This class shows how client code uses the indexer
class Program
{
static void Main(string[] args)
{
SampleCollection<string> stringCollection = new SampleCollection<string>();
stringCollection[0] = "Hello, World";
System.Console.WriteLine(stringCollection[0]);
}
}参考http://msdn.microsoft.com/zh-cn/library/6x16t2tx(VS.80).aspx事件,参考
http://msdn.microsoft.com/zh-cn/library/awbftdfh(VS.80).aspx
get 访问器返回值。set 访问器分配值。
this 关键字用于定义索引器。
value 关键字用于定义由 set 索引器分配的值。
索引器不必根据整数值进行索引,由您决定如何定义特定的查找机制。
索引器可被重载。
索引器可以有多个形参,class Sample
{
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in days)
{
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}
属性其实就是特殊的类成员,它实现了对私有类域的受控访问。在C#语言中有两种属性方法,其一是get,通过它可以返回私有域的值,其二是set,通过它就可以设置私有域的值。public class Student
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Name = "A";
Console.WriteLine(student.Name);
Console.Read();
}
}
类的每一个属性都必须拥有唯一的名称,而类里定义的每一个索引器都必须拥有唯一的签名(signature)或者参数列表(这样就可以实现索引器重载)。
属性可以是static(静态的)而索引器则必须是实例成员。看看msdn