forked from aaubry/YamlDotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/net20'
- Loading branch information
Showing
17 changed files
with
493 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace YamlDotNet.Test | ||
{ | ||
#if NET20 | ||
internal static class Portability | ||
{ | ||
public static SerializerBuilder WithAttributeOverride<TClass>(this SerializerBuilder builder, Expression<Func<TClass, object>> propertyAccessor, Attribute attribute) | ||
{ | ||
return builder.WithAttributeOverride(typeof(TClass), propertyAccessor.AsProperty().Name, attribute); | ||
} | ||
|
||
public static DeserializerBuilder WithAttributeOverride<TClass>(this DeserializerBuilder builder, Expression<Func<TClass, object>> propertyAccessor, Attribute attribute) | ||
{ | ||
return builder.WithAttributeOverride(typeof(TClass), propertyAccessor.AsProperty().Name, attribute); | ||
} | ||
|
||
public static void Add<TClass>(this YamlAttributeOverrides overrides, Expression<Func<TClass, object>> propertyAccessor, Attribute attribute) | ||
{ | ||
var property = propertyAccessor.AsProperty(); | ||
overrides.Add(typeof(TClass), property.Name, attribute); | ||
} | ||
|
||
public static PropertyInfo AsProperty(this LambdaExpression propertyAccessor) | ||
{ | ||
var property = TryGetMemberExpression<PropertyInfo>(propertyAccessor); | ||
if (property == null) | ||
{ | ||
throw new ArgumentException("Expected a lambda expression in the form: x => x.SomeProperty", "propertyAccessor"); | ||
} | ||
|
||
return property; | ||
} | ||
|
||
private static TMemberInfo TryGetMemberExpression<TMemberInfo>(LambdaExpression lambdaExpression) | ||
where TMemberInfo : MemberInfo | ||
{ | ||
if (lambdaExpression.Parameters.Count != 1) | ||
{ | ||
return null; | ||
} | ||
|
||
var body = lambdaExpression.Body; | ||
|
||
var castExpression = body as UnaryExpression; | ||
if (castExpression != null) | ||
{ | ||
if (castExpression.NodeType != ExpressionType.Convert) | ||
{ | ||
return null; | ||
} | ||
|
||
body = castExpression.Operand; | ||
} | ||
|
||
var memberExpression = body as MemberExpression; | ||
if (memberExpression == null) | ||
{ | ||
return null; | ||
} | ||
|
||
if (memberExpression.Expression != lambdaExpression.Parameters[0]) | ||
{ | ||
return null; | ||
} | ||
|
||
return memberExpression.Member as TMemberInfo; | ||
} | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.