Skip to content

Commit 0c3e03a

Browse files
authored
Implement MemberInfo.GetCustomAttributesNative (#98)
***UPDATE_DEPENDENTS*** ***PUBLISH_RELEASE***
1 parent 0cdc0d3 commit 0c3e03a

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

source/nanoFramework.CoreLibrary/System/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
[assembly: AssemblyProduct("nanoFramework mscorlib")]
1515
[assembly: AssemblyCopyright("Copyright © nanoFramework Contributors 2017")]
1616

17-
[assembly: AssemblyNativeVersion("100.4.6.0")]
17+
[assembly: AssemblyNativeVersion("100.4.7.0")]

source/nanoFramework.CoreLibrary/System/Reflection/FieldInfo.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,55 @@ public abstract Type FieldType
6868
/// <summary>
6969
/// When overridden in a derived class, returns an array of all custom attributes applied to this member.
7070
/// </summary>
71-
/// <param name="inherit">true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events.</param>
71+
/// <param name="inherit"><c>true</c> to search this member's inheritance chain to find the attributes; otherwise, <c>false</c>. This parameter is ignored for properties and events.</param>
7272
/// <returns>An array that contains all the custom attributes applied to this member, or an array with zero elements if no attributes are defined.</returns>
73-
/// <remarks>This method is not implemented in nanoFramework.</remarks>
74-
/// <exception cref="NotImplementedException">This method is not implemente.</exception>
75-
/// <exception cref="NotImplementedException"/>
73+
/// <remarks>This method ignores the inherit parameter for properties and events.</remarks>
74+
public override object[] GetCustomAttributes(bool inherit)
75+
{
76+
// get the custom attributes data for the field
77+
// these are returned "encoded" in an object array with 2 positions for each attribute
78+
// 1st the attribute type
79+
// 2nd the constructor parameter or null, if the attribute has no constructor
80+
//
81+
// current limitations:
82+
// - works only for constructors with a single parameter
83+
// - the parameter has to be a string or numeric type
84+
//
85+
// both limitations above can be relatively easily overcome by adding the appropriate code at the native handler
86+
var ret = GetCustomAttributesNative(inherit);
87+
88+
object[] attributes = new object[ ret.Length/2 ];
89+
90+
for (int i = 0; i < ret.Length; i += 2)
91+
{
92+
// peek next element to determine if it's null
93+
if(ret[ i+1 ] == null)
94+
{
95+
// attribute without default constructor, just copy it
96+
attributes[ i/2 ] = ret[i];
97+
}
98+
else
99+
{
100+
// has default constructor, invoke it
101+
102+
// get the types
103+
Type objectType = ret[i].GetType();
104+
Type paramType = ret[ i+1 ].GetType();
105+
106+
// get constructor
107+
ConstructorInfo ctor = objectType.GetConstructor(new Type[] { paramType });
108+
109+
// invoke constructor with the parameter
110+
attributes[ i/2 ] = ctor.Invoke(new object[] { ret[ i+1 ] });
111+
}
112+
}
113+
114+
return attributes;
115+
}
116+
76117
[MethodImpl(MethodImplOptions.InternalCall)]
77118
#pragma warning disable S4200 // Native methods should be wrapped
78-
public extern override object[] GetCustomAttributes(bool inherit);
119+
private extern object[] GetCustomAttributesNative(bool inherit);
79120
#pragma warning restore S4200 // Native methods should be wrapped
80121
}
81122
}

source/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.6.0-preview.{height}",
3+
"version": "1.7.0-preview.{height}",
44
"assemblyVersion": {
55
"precision": "revision"
66
},

0 commit comments

Comments
 (0)