.net知识和学习方法系列(五)关于C#的属性
2008-01-20 13:10
429 查看
一次教学,发现了属性的两个访问器其实是两个方法,于是,就做了个例子来证明一下,代码如下:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Reflection;
6
7namespace Demo
8{
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 ClassA DX = new ClassA();
14 object[] CS1 = new object[0];
15 object[] CS2 = new object[1]{ "这里是参数" };
16 typeof(ClassA).GetMethod("get_SX").Invoke(DX,CS1);
17 typeof(ClassA).GetMethod("set_SX").Invoke(DX, CS2);
18 }
19 }
20
21 class ClassA
22 {
23 public string SX
24 {
25 get
26 {
27 Console.WriteLine("属性的get");
28 return "True";
29 }
30 set
31 {
32 Console.WriteLine("属性的set"+value);
33 }
34 }
35 }
36 }
37在Main方法中,我们用反射来显式的调用属性的get 和set 对应的方法成功了!本文出自 “桂素伟” 博客,请务必保留此出处http://axzxs.blog.51cto.com/730810/149993
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Reflection;
6
7namespace Demo
8{
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 ClassA DX = new ClassA();
14 object[] CS1 = new object[0];
15 object[] CS2 = new object[1]{ "这里是参数" };
16 typeof(ClassA).GetMethod("get_SX").Invoke(DX,CS1);
17 typeof(ClassA).GetMethod("set_SX").Invoke(DX, CS2);
18 }
19 }
20
21 class ClassA
22 {
23 public string SX
24 {
25 get
26 {
27 Console.WriteLine("属性的get");
28 return "True";
29 }
30 set
31 {
32 Console.WriteLine("属性的set"+value);
33 }
34 }
35 }
36 }
37在Main方法中,我们用反射来显式的调用属性的get 和set 对应的方法成功了!本文出自 “桂素伟” 博客,请务必保留此出处http://axzxs.blog.51cto.com/730810/149993
相关文章推荐
- .net知识和学习方法系列(五)关于C#的属性
- .net知识和学习方法系列(六)关于数值类型
- .net是如何实现Page.IsPostBack属性的?看看C#中关于IsPostBack的解释
- .net知识和学习方法系列(二十二)CLR-方法的参数out,ref
- C#基础知识梳理系列三:C#类成员:常量、字段、属性
- .net知识和学习方法系列(二十三)嵌套类
- .net知识和学习方法系列(二)CSharp的程序结构
- .net知识和学习方法系列(二十)CLR-委托
- .net知识和学习方法系列(九)委托异步调用方法a
- .net知识和学习方法系列(十二)窗体的讲解与学习
- .net知识和学习方法系列(二十二)CLR-方法的参数out,ref
- .net知识和学习方法系列(二十三)嵌套类
- .net知识和学习方法系列(前言)
- C#WinFrom开发系列之关于动态添加生成和删除控件的相关知识
- .NET面试题系列[3] - C# 基础知识(1)
- .net知识和学习方法系列(十二)窗体的讲解与学习
- .Net(C#)开发漫谈:关于变量的命名和属性
- .NET/C# 中关于属性(Property)的两个小知识点
- .net知识和学习方法系列(八)static
- .net知识和学习方法系列(前言)