-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (107 loc) · 5.18 KB
/
Program.cs
File metadata and controls
131 lines (107 loc) · 5.18 KB
1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//==== Author: janluksoft@interia.pl ==========================
using System;
using System.Reflection;
namespace NameReflection
{
class Program
{
static void Main(string[] args)
{
var plane = Activator.CreateInstance(typeof(Aircraft)) as Aircraft;
// Access to class properties
PropertyInfo[] properties = plane.GetType().GetProperties();
PropertyInfo prop1 = properties[0];
PropertyInfo prop2 = properties[1];
PropertyInfo prop3 = properties[2];
// Setting the values of individual fields
prop1.SetValue(plane, "Airbus A320"); //Name
prop2.SetValue(plane, 64000); //Mass
prop3.SetValue(plane, 868.8); //Speed
// Display value
Console.WriteLine($"An example of manipulating objects via the Reflection mechanism.\n\r");
Console.WriteLine($"Create instance \"plane\" by Reflection:");
string mess = $"Aircraft name: {prop1.GetValue(plane, null)}," +
$" mass: {prop2.GetValue(plane, null)} kg, speed: {prop3.GetValue(plane, null)} km/h.";
Console.WriteLine("Info by properties: " + mess);
// Access to the constructors
var Constructor1 = typeof(Aircraft).GetConstructor(new Type[0]);
var Constructor2 = typeof(Aircraft).GetConstructor(new[] { typeof(int) });
var Constructor3 = typeof(Aircraft).GetConstructor(new[] { typeof(string), typeof(int), typeof(double) });
// creating new instances by calling different constructors
var InstancePlane1 = (Aircraft)Constructor1.Invoke(null);
var InstancePlane2 = (Aircraft)Constructor2.Invoke(new object[] { 7000 });
var InstancePlane3 = (Aircraft)Constructor3.Invoke(new object[] { "Boeing B737", 50000, 859.2 });
// Displaying the values of created instances
PrintInstanceValues("Info by instance plane: ", plane);
Console.WriteLine($"\n\rCreate instances by Reflection:");
PrintInstanceValues("Info by created plane1: ", InstancePlane1);
PrintInstanceValues("Info by created plane2: ", InstancePlane2);
PrintInstanceValues("Info by created plane3: ", InstancePlane3);
Console.WriteLine("");
// Access to the methods
var Method1 = typeof(Aircraft).GetMethod("SetName");
var InvokeMethod1 = (Aircraft)Method1.Invoke(plane, new object[] { "Embrader" });
PrintInstanceValues("Info by instance plane: ", plane);
var Method2 = typeof(Aircraft).GetMethod("SetMass");
var InvokeMethod2 = (Aircraft)Method2.Invoke(plane, new object[] { 7000 });
PrintInstanceValues("Info by instance plane: ", plane);
Console.ReadKey();
// Program messages in the console:
// An example of manipulating objects via the Reflection mechanism.
// Create instance "plane" by Reflection:
// Info by properties: Aircraft name: Airbus A320, mass: 64000 kg, speed: 868,8 km/h.
// Info by instance plane: Aircraft name: Airbus A320, mass: 64000 kg, speed: 868,8 km/h.
// Create instances by Reflection:
// Info by created plane1: Aircraft name: , mass: 0 kg, speed: 0 km/h.
// Info by created plane2: Aircraft name: , mass: 7000 kg, speed: 0 km/h.
// Info by created plane3: Aircraft name: Boeing B737, mass: 50000 kg, speed: 859,2 km/h.
// Reflection, Info from method SetName: Embrader
// Info by instance plane: Aircraft name: Embrader, mass: 64000 kg, speed: 868,8 km/h.
// Reflection, Info from method SetMass(): 7000 kg.
// Info by instance plane: Aircraft name: Embrader, mass: 7000 kg, speed: 868,8 km/h.
}
static void PrintInstanceValues(string xInfo, Aircraft xplane)
{
string mess = $"Aircraft name: {xplane.Name}," +
$" mass: {xplane.Mass} kg, speed: {xplane.Speed} km/h.";
Console.WriteLine(xInfo + mess);
}
static string GetValProp(PropertyInfo xProp, Aircraft xpl)
{
return (xProp.GetValue(xpl, null).ToString());
}
}
class Aircraft
{
public string Name { get; set; }
public int Mass { get; set; }
public double Speed { get; set; }
public Aircraft()
{
}
public Aircraft(int mass)
{
Mass = mass;
}
public Aircraft(string name, int mass, double speed)
{
Name = name;
Mass = mass;
Speed = speed;
}
public void SetName(string name)
{
this.Name = name;
Console.WriteLine($"Reflection, Info from method SetName: {this.Name}");
}
public void SetMass(int mass)
{
this.Mass = mass;
Console.WriteLine($"Reflection, Info from method SetMass(): {this.Mass} kg.");
}
public void Message()
{
Console.WriteLine($"Aircraft name: {this.Name}, mass: {this.Mass} kg, speed: {this.Speed} km/h.");
}
}
}