Skip to content

Commit dfc0640

Browse files
committed
Fix build issues related to PR #29.
1 parent 02143bc commit dfc0640

File tree

3 files changed

+154
-28
lines changed

3 files changed

+154
-28
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,7 @@ Release 0.4.5
116116

117117
BUF FIXES
118118
* Fix creating serializer for abstract dictionary types causes exception in WinRT.
119+
120+
NEW FEATURES
121+
* System.Runtime.Serialization assembly is not required now (pull-request #29). Thanks @takeshik!
122+
* Read only members now ignored (issue #28, pull-request #30). hanks @takeshik!

src/MsgPack/ReflectionAbstractions.cs

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
using System;
2222
using System.Collections.Generic;
23+
using System.Diagnostics.Contracts;
2324
using System.Linq;
2425
using System.Reflection;
2526

@@ -194,6 +195,26 @@ public static bool IsDefined( this Type source, Type attributeType )
194195
{
195196
return source.GetTypeInfo().IsDefined( attributeType );
196197
}
198+
199+
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this Type source )
200+
{
201+
return source.GetTypeInfo().CustomAttributes;
202+
}
203+
204+
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this MemberInfo source )
205+
{
206+
return source.CustomAttributes;
207+
}
208+
209+
public static Type GetAttributeType( this CustomAttributeData source )
210+
{
211+
return source.AttributeType;
212+
}
213+
214+
public static string GetMemberName( this CustomAttributeNamedArgument source )
215+
{
216+
return source.MemberName;
217+
}
197218
#else
198219
public static bool IsDefined( this MemberInfo source, Type attributeType )
199220
{
@@ -204,7 +225,100 @@ public static T GetCustomAttribute<T>( this MemberInfo source )
204225
where T : Attribute
205226
{
206227
return Attribute.GetCustomAttribute( source, typeof( T ) ) as T;
207-
}
228+
}
229+
230+
#if !SILVERLIGHT
231+
public static Type GetAttributeType( this CustomAttributeData source )
232+
{
233+
return source.Constructor.DeclaringType;
234+
}
235+
236+
public static string GetMemberName( this CustomAttributeNamedArgument source )
237+
{
238+
return source.MemberInfo.Name;
239+
}
240+
241+
#else
242+
public static Type GetAttributeType( this Attribute source )
243+
{
244+
return source.GetType();
245+
}
246+
#endif // !SILVERLIGHT
247+
#endif
248+
249+
#if NETFX_35
250+
public static IEnumerable<CustomAttributeData> GetCustomAttributesData( this MemberInfo source )
251+
{
252+
return CustomAttributeData.GetCustomAttributes( source );
253+
}
254+
#endif // NETFX_35
255+
256+
#if SILVERLIGHT
257+
public static IEnumerable<Attribute> GetCustomAttributesData( this MemberInfo source )
258+
{
259+
return source.GetCustomAttributes( false ).OfType<Attribute>();
260+
}
261+
262+
public static IEnumerable<NamedArgument> GetNamedArguments( this Attribute attribute )
263+
{
264+
return
265+
attribute.GetType()
266+
.GetMembers( BindingFlags.Public | BindingFlags.Instance )
267+
.Where( m => m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property )
268+
.Select( m => new NamedArgument( attribute, m ) );
269+
}
270+
#else
271+
public static IEnumerable<CustomAttributeNamedArgument> GetNamedArguments( this CustomAttributeData source )
272+
{
273+
return source.NamedArguments;
274+
}
275+
276+
public static CustomAttributeTypedArgument GetTypedValue( this CustomAttributeNamedArgument source )
277+
{
278+
return source.TypedValue;
279+
}
280+
#endif // SILVERLIGHT
281+
282+
#if SILVERLIGHT
283+
public struct NamedArgument
284+
{
285+
private object _instance;
286+
private MemberInfo _memberInfo;
287+
288+
public NamedArgument( object instance, MemberInfo memberInfo )
289+
{
290+
this._instance = instance;
291+
this._memberInfo = memberInfo;
292+
}
293+
294+
public string GetMemberName()
295+
{
296+
return this._memberInfo.Name;
297+
}
298+
299+
public KeyValuePair<Type, object> GetTypedValue()
300+
{
301+
Type type;
302+
object value;
303+
PropertyInfo asProperty;
304+
if ( ( asProperty = this._memberInfo as PropertyInfo ) != null )
305+
{
306+
type = asProperty.PropertyType;
307+
value = asProperty.GetValue( this._instance, null );
308+
}
309+
else
310+
{
311+
var asField = this._memberInfo as FieldInfo;
312+
#if DEBUG
313+
Contract.Assert( asField != null );
314+
#endif
315+
type = asField.FieldType;
316+
value = asField.GetValue( this._instance );
317+
}
318+
319+
return new KeyValuePair<Type, object>( type, value );
320+
}
321+
}
208322
#endif
209323
}
210324
}

src/MsgPack/Serialization/SerializerBuilder`1.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace MsgPack.Serialization
3232
/// Build serializer for <typeparamref name="TObject"/>.
3333
/// </summary>
3434
/// <typeparam name="TObject">Object to be serialized/deserialized.</typeparam>
35-
[ContractClass(typeof(SerializerBuilderContract<>))]
35+
[ContractClass( typeof( SerializerBuilderContract<> ) )]
3636
internal abstract class SerializerBuilder<TObject>
3737
{
3838
private readonly SerializationContext _context;
@@ -95,8 +95,8 @@ public MessagePackSerializer<TObject> CreateSerializer()
9595
if ( this.Context.CompatibilityOptions.OneBoundDataMemberOrder && entries[ 0 ].Contract.Id == 0 )
9696
{
9797
throw new NotSupportedException( "Cannot specify order value 0 on DataMemberAttribute when SerializationContext.CompatibilityOptions.OneBoundDataMemberOrder is set to true." );
98-
}
99-
98+
}
99+
100100
var maxId = entries.Max( item => item.Contract.Id );
101101
var result = new List<SerializingMember>( maxId + 1 );
102102
for ( int source = 0, destination = this.Context.CompatibilityOptions.OneBoundDataMemberOrder ? 1 : 0; source < entries.Length; source++, destination++ )
@@ -149,31 +149,39 @@ private static IEnumerable<SerializingMember> GetTargetMembers()
149149
);
150150
}
151151

152-
if ( typeof( TObject ).GetCustomAttributes( false ).Any( attr => attr.GetType().FullName == "System.Runtime.Serialization.DataContractAttribute" ) )
152+
if ( typeof( TObject ).GetCustomAttributesData().Any( attr =>
153+
attr.GetAttributeType().FullName == "System.Runtime.Serialization.DataContractAttribute" ) )
153154
{
154-
return members.Select( item => new
155-
{
156-
member = item,
157-
data = item.GetCustomAttributesData()
158-
.FirstOrDefault( data => data.Constructor.DeclaringType.FullName == "System.Runtime.Serialization.DataMemberAttribute" )
159-
})
160-
.Where( item => item.data != null )
161-
.Select( item =>
162-
{
163-
var name = item.data.NamedArguments
164-
.Where( arg => arg.MemberInfo.Name == "Name" )
165-
.Select( arg => (string) arg.TypedValue.Value )
166-
.FirstOrDefault();
167-
var id = item.data.NamedArguments
168-
.Where( arg => arg.MemberInfo.Name == "Order" )
169-
.Select( arg => (int?) arg.TypedValue.Value )
170-
.FirstOrDefault();
171-
172-
return new SerializingMember(
173-
item.member,
174-
new DataMemberContract( item.member, name, NilImplication.MemberDefault, id )
155+
return
156+
members.Select(
157+
item =>
158+
new
159+
{
160+
member = item,
161+
data = item.GetCustomAttributesData()
162+
.FirstOrDefault(
163+
data => data.GetAttributeType().FullName == "System.Runtime.Serialization.DataMemberAttribute" )
164+
}
165+
).Where( item => item.data != null )
166+
.Select(
167+
item =>
168+
{
169+
var name = item.data.GetNamedArguments()
170+
.Where( arg => arg.GetMemberName() == "Name" )
171+
.Select( arg => ( string )arg.GetTypedValue().Value )
172+
.FirstOrDefault();
173+
var id = item.data.GetNamedArguments()
174+
.Where( arg => arg.GetMemberName() == "Order" )
175+
.Select( arg => ( int? )arg.GetTypedValue().Value )
176+
.FirstOrDefault();
177+
178+
return
179+
new SerializingMember(
180+
item.member,
181+
new DataMemberContract( item.member, name, NilImplication.MemberDefault, id )
182+
);
183+
}
175184
);
176-
});
177185
}
178186

179187
#if SILVERLIGHT || NETFX_CORE
@@ -223,7 +231,7 @@ private static IEnumerable<SerializingMember> GetTargetMembers()
223231
public abstract MessagePackSerializer<TObject> CreateTupleSerializer();
224232
}
225233

226-
[ContractClassFor(typeof(SerializerBuilder<>))]
234+
[ContractClassFor( typeof( SerializerBuilder<> ) )]
227235
internal abstract class SerializerBuilderContract<T> : SerializerBuilder<T>
228236
{
229237
protected SerializerBuilderContract() : base( null ) { }

0 commit comments

Comments
 (0)