.net知识和学习方法系列(五)关于C#的属性
一次教学,发现了属性的两个访问器其实是两个方法,于是,就做了个例子来证明一下,代码如下:
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Reflection;
6
7
namespace 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 对应的方法成功了!

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

****欢迎关注我的asp.net core系统课程****
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524
《asp.net core精要讲解》 https://ke.qq.com/course/265696
《asp.net core 3.0》 https://ke.qq.com/course/437517
《asp.net core项目实战》 https://ke.qq.com/course/291868
《基于.net core微服务》 https://ke.qq.com/course/299524