2
using System.Reflection;
3
using csharp_attributesNamespace;
9
// Custom attributes typemap tests
11
// cstype typemap attributechecks
13
// Global function cstype typemap attributes check
14
Type globaltype = typeof(csharp_attributes);
16
MethodInfo member = (MethodInfo)globaltype.GetMember("GlobalFunction")[0];
17
if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
18
throw new Exception("No IntOut attribute for " + member.Name);
19
ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
20
if (parameter.Name != "myInt")
21
throw new Exception("Incorrect parameter name");
22
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
23
if (attribute.GetType() != typeof(IntInAttribute))
24
throw new Exception("Expecting IntIn attribute");
26
// Constant - cstype typemap attributes check
28
MemberInfo member = (MemberInfo)globaltype.GetMember("TESTMACRO")[0];
29
if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
30
throw new Exception("No IntOut attribute for " + member.Name);
33
// Non-static method cstype typemap attributes check
34
Type type = typeof(Stations);
36
MethodInfo member = (MethodInfo)type.GetMember("Reading")[0];
37
if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
38
throw new Exception("No IntOut attribute for " + member.Name);
39
ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
40
if (parameter.Name != "myInt")
41
throw new Exception("Incorrect parameter name");
42
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
43
if (attribute.GetType() != typeof(IntInAttribute))
44
throw new Exception("Expecting IntIn attribute");
46
// Static method cstype typemap attributes check
48
MethodInfo member = (MethodInfo)type.GetMember("Swindon")[0];
49
if (Attribute.GetCustomAttribute(member, typeof(IntOutAttribute)) == null)
50
throw new Exception("No IntOut attribute for " + member.Name);
51
ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
52
if (parameter.Name != "myInt")
53
throw new Exception("Incorrect parameter name");
54
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
55
if (attribute.GetType() != typeof(IntInAttribute))
56
throw new Exception("Expecting IntIn attribute");
58
// Constructor cstype typemap attributes check
60
ConstructorInfo member = (ConstructorInfo)type.GetConstructors()[0];
61
ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter
62
if (parameter.Name != "myInt")
63
throw new Exception("Incorrect parameter name");
64
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
65
if (attribute.GetType() != typeof(IntInAttribute))
66
throw new Exception("Expecting IntIn attribute");
70
// imtype typemap attributechecks
72
// Global function imtype typemap attributes check
73
Type imclasstype = typeof(csharp_attributesPINVOKE);
75
MethodInfo member = (MethodInfo)imclasstype.GetMember("GlobalFunction")[0];
76
if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
77
throw new Exception("No IntegerOut attribute for " + member.Name);
78
ParameterInfo parameter = member.GetParameters()[0]; // checking 1st parameter
79
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
80
if (attribute.GetType() != typeof(IntegerInAttribute))
81
throw new Exception("Expecting IntegerIn attribute");
83
// Constant - imtype typemap attributes check
85
MethodInfo member = (MethodInfo)imclasstype.GetMember("TESTMACRO_get")[0];
86
if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
87
throw new Exception("No IntegerOut attribute for " + member.Name);
89
// Non-static method imtype typemap attributes check
91
MethodInfo member = (MethodInfo)imclasstype.GetMember("Stations_Reading")[0];
92
if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
93
throw new Exception("No IntegerOut attribute for " + member.Name);
94
ParameterInfo parameter = member.GetParameters()[1]; // checking 2nd parameter
95
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
96
if (attribute.GetType() != typeof(IntegerInAttribute))
97
throw new Exception("Expecting IntegerIn attribute");
99
// Static method imtype typemap attributes check
101
MethodInfo member = (MethodInfo)imclasstype.GetMember("Stations_Swindon")[0];
102
if (Attribute.GetCustomAttribute(member, typeof(IntegerOutAttribute)) == null)
103
throw new Exception("No IntegerOut attribute for " + member.Name);
104
ParameterInfo parameter = member.GetParameters()[0]; // checking 1st parameter
105
Attribute attribute = Attribute.GetCustomAttributes(parameter)[0];
106
if (attribute.GetType() != typeof(IntegerInAttribute))
107
throw new Exception("Expecting IntegerIn attribute");
111
// attributes feature
113
Type moretype = typeof(MoreStations);
115
// Constructor attributes feature check
117
ConstructorInfo member = (ConstructorInfo)moretype.GetConstructors()[0];
118
if (Attribute.GetCustomAttribute(member, typeof(InterCity1Attribute)) == null)
119
throw new Exception("MoreStations::MoreStations attribute failed");
121
// Non-static method attributes feature check
123
MethodInfo member = (MethodInfo)moretype.GetMember("Chippenham")[0];
124
if (Attribute.GetCustomAttribute(member, typeof(InterCity2Attribute)) == null)
125
throw new Exception("MoreStations::Chippenham attribute failed");
127
// Static method attributes feature check
129
MethodInfo member = (MethodInfo)moretype.GetMember("Bath")[0];
130
if (Attribute.GetCustomAttribute(member, typeof(InterCity3Attribute)) == null)
131
throw new Exception("MoreStations::Bath attribute failed");
133
// Non-static member variable attributes feature check
135
PropertyInfo member = (PropertyInfo)moretype.GetProperty("Bristol");
136
if (Attribute.GetCustomAttribute(member, typeof(InterCity4Attribute)) == null)
137
throw new Exception("MoreStations::Bristol attribute failed");
139
// Static member variable attributes feature check
141
PropertyInfo member = (PropertyInfo)moretype.GetProperty("WestonSuperMare");
142
if (Attribute.GetCustomAttribute(member, typeof(InterCity5Attribute)) == null)
143
throw new Exception("MoreStations::Bristol attribute failed");
145
// Global function attributes feature check
147
MethodInfo member = (MethodInfo)globaltype.GetMember("Paddington")[0];
148
if (Attribute.GetCustomAttribute(member, typeof(InterCity7Attribute)) == null)
149
throw new Exception("MoreStations::Paddington attribute failed");
151
// Global variables attributes feature check
153
PropertyInfo member = (PropertyInfo)globaltype.GetProperty("DidcotParkway");
154
if (Attribute.GetCustomAttribute(member, typeof(InterCity8Attribute)) == null)
155
throw new Exception("MoreStations::Paddington attribute failed");
159
// csattribute typemaps
161
// Class csattribute typemap
163
Object[] attribs = moretype.GetCustomAttributes(true);
164
Eurostar1Attribute tgv = (Eurostar1Attribute)attribs[0];
166
throw new Exception("No attribute for MoreStations");
168
// Nested enum csattribute typemap
170
MemberInfo member = (MemberInfo)moretype.GetMember("Wales")[0];
171
if (Attribute.GetCustomAttribute(member, typeof(Eurostar2Attribute)) == null)
172
throw new Exception("No attribute for " + member.Name);
174
// Enum csattribute typemap
176
Type cymrutype = typeof(Cymru);
177
Object[] attribs = cymrutype.GetCustomAttributes(true);
178
Eurostar3Attribute tgv = (Eurostar3Attribute)attribs[0];
180
throw new Exception("No attribute for Cymru");
185
// Custom attribute classes
186
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
187
public class IntInAttribute : Attribute {}
189
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
190
public class IntOutAttribute : Attribute {}
192
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
193
public class IntegerInAttribute : Attribute {}
195
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
196
public class IntegerOutAttribute : Attribute {}
199
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
200
public class InterCity1Attribute : Attribute {}
202
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
203
public class InterCity2Attribute : Attribute {}
205
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
206
public class InterCity3Attribute : Attribute {}
208
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
209
public class InterCity4Attribute : Attribute {}
211
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
212
public class InterCity5Attribute : Attribute {}
214
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
215
public class InterCity6Attribute : Attribute {}
217
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
218
public class InterCity7Attribute : Attribute {}
220
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
221
public class InterCity8Attribute : Attribute {}
224
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
225
public class Eurostar1Attribute : Attribute {}
227
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
228
public class Eurostar2Attribute : Attribute {}
230
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
231
public class Eurostar3Attribute : Attribute {}
234
[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
235
public class ThreadSafeAttribute : Attribute {
236
public ThreadSafeAttribute(bool safe) {}
237
public ThreadSafeAttribute() {}