Skip to content

Commit afef3bf

Browse files
committed
Update Reflection sample
- Add code to demo custom attributes with multiple parameter constructors. - Update NuGet.
1 parent 02deec0 commit afef3bf

File tree

8 files changed

+155
-10
lines changed

8 files changed

+155
-10
lines changed

samples/Reflection/CustomAttributes.Desktop/Program.cs

+62-4
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,83 @@ static void Main(string[] args)
1616
Type myType = typeof(MyClass1);
1717

1818
// Display the attributes of MyClass1.
19-
Object[] myAttributes = myType.GetCustomAttributes(true);
19+
object[] myAttributes = myType.GetCustomAttributes(true);
2020
if (myAttributes.Length > 0)
2121
{
2222
Console.WriteLine($"\nThe attributes for the class '{myType.Name}' are:");
23-
for (int j = 0; j < myAttributes.Length; j++)
23+
for (int j = myAttributes.Length - 1; j >= 0; j--)
24+
{
2425
Console.WriteLine($" {myAttributes[j]}");
26+
}
2527
}
2628

2729
// Get the methods associated with MyClass1.
2830
MemberInfo[] myMethods = myType.GetMethods();
2931

32+
Console.WriteLine("");
33+
Console.WriteLine($"'{myType.Name}' type has '{myMethods.Length}' methods");
34+
3035
// Display the attributes for each of the methods of MyClass1.
3136
for (int i = 0; i < myMethods.Length; i++)
3237
{
38+
Console.WriteLine("");
39+
Console.WriteLine($"Getting custom attributes for '{myMethods[i].Name}'");
40+
3341
myAttributes = myMethods[i].GetCustomAttributes(true);
42+
43+
Console.WriteLine("");
44+
Console.WriteLine($"'{myMethods[i].Name}' method has {myAttributes.Length} custom attributes");
45+
3446
if (myAttributes.Length > 0)
3547
{
3648
Console.WriteLine($"\nThe attributes for the method '{myMethods[i].Name}' of class '{myType.Name}' are:");
49+
3750
for (int j = 0; j < myAttributes.Length; j++)
51+
{
3852
Console.WriteLine($" {myAttributes[j]}");
39-
40-
var attributeData = myMethods[i].GetCustomAttributesData();
53+
}
54+
55+
foreach (var attrib in myAttributes)
56+
{
57+
// check if the method has Attribute1
58+
if (attrib is Attribute1Attribute)
59+
{
60+
Console.WriteLine($" >>>>>>> {myMethods[i].Name} has 'Attribute1' attribute");
61+
}
62+
63+
// check if the method has IgnoreAttribute
64+
if (attrib is IgnoreAttribute)
65+
{
66+
Console.WriteLine($" >>>>>>> {myMethods[i].Name} has 'IgnoreAttribute' attribute");
67+
}
68+
69+
// check if the method has DataRowAttribute
70+
if (attrib is DataRowAttribute)
71+
{
72+
Console.WriteLine($" >>>>>>> {myMethods[i].Name} has 'DataRowAttribute' attribute");
73+
74+
DataRowAttribute attDataRow = (DataRowAttribute)attrib;
75+
76+
int index = 0;
77+
78+
foreach(var dataRow in attDataRow.Arguments)
79+
{
80+
Console.WriteLine($" DataRowAttribute.Arg[{index++}] has: {dataRow}");
81+
}
82+
}
83+
84+
// check if the method has ComplexAttribute
85+
if (attrib is ComplexAttribute)
86+
{
87+
Console.WriteLine($" >>>>>>> {myMethods[i].Name} has 'ComplexAttribute' attribute");
88+
89+
ComplexAttribute attDataRow = (ComplexAttribute)attrib;
90+
91+
Console.WriteLine($" ComplexAttribute.Max is {attDataRow.Max}");
92+
Console.WriteLine($" ComplexAttribute.B is {attDataRow.B}");
93+
Console.WriteLine($" ComplexAttribute.S is {attDataRow.S}");
94+
}
95+
}
4196
}
4297
}
4398

@@ -52,8 +107,11 @@ static void Main(string[] args)
52107
if (myAttributes.Length > 0)
53108
{
54109
Console.WriteLine($"\nThe attributes for field '{myFields[i].Name}' of class '{myType.Name}' are:");
110+
55111
for (int j = 0; j < myFields.Length; j++)
112+
{
56113
Console.WriteLine($" {myFields[j]}");
114+
}
57115

58116
var attributeData = myFields[i].GetCustomAttributesData();
59117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
8+
namespace Reflection.CustomAttributes
9+
{
10+
public class ComplexAttribute : Attribute
11+
{
12+
private readonly uint _max;
13+
private readonly string _s;
14+
private readonly bool _b;
15+
16+
public uint Max => _max;
17+
18+
public string S => _s;
19+
public bool B => _b;
20+
21+
public ComplexAttribute(uint m, string s, bool b)
22+
{
23+
_max = m;
24+
_s = s;
25+
_b = b;
26+
}
27+
}
28+
}

samples/Reflection/CustomAttributes.Shared/CustomAttributes.Shared.projitems

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<Import_RootNamespace>CustomAttributes.Shared</Import_RootNamespace>
1010
</PropertyGroup>
1111
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)ComplexAttribute.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)DataRowAttribute.cs" />
1214
<Compile Include="$(MSBuildThisFileDirectory)AuthorAttribute.cs" />
1315
<Compile Include="$(MSBuildThisFileDirectory)IgnoreAttribute.cs" />
1416
<Compile Include="$(MSBuildThisFileDirectory)MaxAttribute.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
8+
namespace Reflection.CustomAttributes
9+
{
10+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
11+
public class DataRowAttribute : Attribute
12+
{
13+
public DataRowAttribute(params object[] args)
14+
{
15+
Arguments = args;
16+
}
17+
18+
public object[] Arguments { get; }
19+
}
20+
}

samples/Reflection/CustomAttributes.Shared/MyClass1.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace Reflection.CustomAttributes
1010
[Attribute4]
1111
public class MyClass1
1212
{
13-
1413
[Attribute1]
1514
[Attribute3]
1615
public void MyMethod1(int i)
@@ -24,6 +23,12 @@ public void MyMethodToIgnore()
2423

2524
}
2625

26+
[DataRow((int)-1, (byte)2, (long)345678, (string)"A string", (bool)true)]
27+
[Complex(0xBEEF, "Another string", false)]
28+
public void MyMethodWithData()
29+
{
30+
}
31+
2732
private readonly int _myField;
2833

2934
[Ignore("I'm ignoring you!")]

samples/Reflection/CustomAttributes/CustomAttributes.nfproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</ItemGroup>
2929
<ItemGroup>
3030
<Reference Include="mscorlib">
31-
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0-preview.9\lib\mscorlib.dll</HintPath>
31+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0-preview.18\lib\mscorlib.dll</HintPath>
3232
</Reference>
3333
</ItemGroup>
3434
<ItemGroup>

samples/Reflection/CustomAttributes/Program.cs

+35-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ public static void Main()
1818
Type myType = typeof(MyClass1);
1919

2020
// Display the attributes of MyClass1.
21-
Object[] myAttributes = myType.GetCustomAttributes(true);
21+
object[] myAttributes = myType.GetCustomAttributes(true);
2222
if (myAttributes.Length > 0)
2323
{
2424
Debug.WriteLine($"\nThe attributes for the class '{myType.Name}' are:");
2525
for (int j = 0; j < myAttributes.Length; j++)
26+
{
2627
Debug.WriteLine($" {myAttributes[j]}");
28+
}
2729
}
2830

2931
// Get the methods associated with MyClass1.
@@ -33,24 +35,54 @@ public static void Main()
3335
for (int i = 0; i < myMethods.Length; i++)
3436
{
3537
myAttributes = myMethods[i].GetCustomAttributes(true);
38+
3639
if (myAttributes.Length > 0)
3740
{
3841
Debug.WriteLine($"\nThe attributes for the method '{myMethods[i].Name}' of class '{myType.Name}' are:");
42+
3943
for (int j = 0; j < myAttributes.Length; j++)
4044
{
4145
Debug.WriteLine($" {myAttributes[j]}");
4246

4347
// check if the method has Attribute1
44-
if (typeof(Attribute1Attribute).Equals(myAttributes[j]))
48+
if (myAttributes[j] is Attribute1Attribute)
4549
{
4650
Debug.WriteLine($" >>>>>>> {myMethods[i].Name} has 'Attribute1' attribute");
4751
}
4852

4953
// check if the method has IgnoreAttribute
50-
if (typeof(IgnoreAttribute).Equals(myAttributes[j]))
54+
if (myAttributes[j] is IgnoreAttribute)
5155
{
5256
Debug.WriteLine($" >>>>>>> {myMethods[i].Name} has 'IgnoreAttribute' attribute");
5357
}
58+
59+
// check if the method has DataRowAttribute
60+
if (myAttributes[j] is DataRowAttribute)
61+
{
62+
Debug.WriteLine($" >>>>>>> {myMethods[i].Name} has 'DataRowAttribute' attribute");
63+
64+
DataRowAttribute attDataRow = (DataRowAttribute)myAttributes[j];
65+
66+
int index = 0;
67+
68+
foreach (var dataRow in attDataRow.Arguments)
69+
{
70+
Console.WriteLine($" DataRowAttribute.Arg[{index++}] has: {dataRow}");
71+
}
72+
}
73+
74+
// check if the method has ComplexAttribute
75+
if (myAttributes[j] is ComplexAttribute)
76+
{
77+
Debug.WriteLine($" >>>>>>> {myMethods[i].Name} has 'ComplexAttribute' attribute");
78+
79+
ComplexAttribute attDataRow = (ComplexAttribute)myAttributes[j];
80+
81+
Console.WriteLine($" ComplexAttribute.Max is {attDataRow.Max}");
82+
Console.WriteLine($" ComplexAttribute.B is {attDataRow.B}");
83+
Console.WriteLine($" ComplexAttribute.S is {attDataRow.S}");
84+
}
85+
5486
}
5587
}
5688
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.9" targetFramework="netnanoframework10" />
3+
<package id="nanoFramework.CoreLibrary" version="1.12.0-preview.18" targetFramework="netnanoframework10" />
44
</packages>

0 commit comments

Comments
 (0)