Skip to content

Commit

Permalink
Refactored CK.Reflection: no more oject/type extension methods.
Browse files Browse the repository at this point in the history
Renamed ObjectAndTypeExtension to DelegateHelper that now supports CreateSetter methods.
  • Loading branch information
olivier-spinelli committed Apr 18, 2016
1 parent 8d6df7f commit 8441e0e
Show file tree
Hide file tree
Showing 8 changed files with 493 additions and 760 deletions.
3 changes: 1 addition & 2 deletions CK.Reflection/CK.Reflection.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@
<Compile Include="EmitHelper.cs" />
<Compile Include="ILGeneratorExtension.cs" />
<Compile Include="InternalResources.cs" />
<Compile Include="LegacySupport\CustomAttributeExtensions.cs" />
<Compile Include="MethodInfoEqualityComparer.cs" />
<Compile Include="ObjectAndTypeExtension.cs" />
<Compile Include="DelegateHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionHelper.cs" />
<Compile Include="TypeBuilderExtension.cs" />
Expand Down

Large diffs are not rendered by default.

253 changes: 0 additions & 253 deletions CK.Reflection/LegacySupport/CustomAttributeExtensions.cs

This file was deleted.

74 changes: 4 additions & 70 deletions CK.Reflection/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,6 @@ namespace CK.Reflection
/// </summary>
static public class ReflectionHelper
{
/// <summary>
/// Describes the behavior of <see cref="M:CreateSetter"/> methods when no setter exists
/// on the property.
/// </summary>
public enum CreateInvalidSetterOption
{
/// <summary>
/// Throws an <see cref="InvalidOperationException"/>. This is the default.
/// </summary>
ThrowException,
/// <summary>
/// Returns a null action delegate.
/// </summary>
NullAction,
/// <summary>
/// Returns a void action (an action that does nothing).
/// </summary>
VoidAction
}

/// <summary>
/// Retrieves a <see cref="PropertyInfo"/> from a lambda function based on an instance of the holder.
/// </summary>
Expand All @@ -70,20 +50,6 @@ public static PropertyInfo GetPropertyInfo<THolder, TProperty>( THolder source,
return DoGetPropertyInfo( propertyLambda );
}

/// <summary>
/// Creates a setter for a property.
/// </summary>
/// <typeparam name="THolder">Property holder type (will be inferred by the compiler).</typeparam>
/// <typeparam name="TProperty">Property type (will be inferred by the compiler).</typeparam>
/// <param name="source">An instance of the <typeparamref name="THolder"/>.</param>
/// <param name="propertyLambda">A lambda function that selects the property.</param>
/// <param name="o">Error handling options. Defaults to <see cref="CreateInvalidSetterOption.ThrowException"/>.</param>
/// <returns>An action that takes an holder instance and the value to set.</returns>
public static Action<THolder, TProperty> CreateSetter<THolder, TProperty>( THolder source, Expression<Func<THolder, TProperty>> propertyLambda, CreateInvalidSetterOption o = CreateInvalidSetterOption.ThrowException )
{
return CreateSetter<THolder,TProperty>( DoGetPropertyInfo( propertyLambda ), o );
}

/// <summary>
/// Retrieves a <see cref="PropertyInfo"/> from a lambda function without requiring an instance of the holder
/// object and without any constraint for the type of the property.
Expand All @@ -108,19 +74,6 @@ public static PropertyInfo GetPropertyInfo<THolder, TProperty>( Expression<Func<
return DoGetPropertyInfo( propertyLambda );
}

/// <summary>
/// Creates a setter fo a property.
/// </summary>
/// <typeparam name="THolder">Property holder type.</typeparam>
/// <typeparam name="TProperty">Property type.</typeparam>
/// <param name="propertyLambda">A lambda function that selects the property.</param>
/// <param name="o">Error handling options. Defaults to <see cref="CreateInvalidSetterOption.ThrowException"/>.</param>
/// <returns>An action that takes an holder instance and the value to set.</returns>
public static Action<THolder, TProperty> CreateSetter<THolder, TProperty>( Expression<Func<THolder, TProperty>> propertyLambda, CreateInvalidSetterOption o = CreateInvalidSetterOption.ThrowException )
{
return CreateSetter<THolder, TProperty>( DoGetPropertyInfo( propertyLambda ), o );
}

/// <summary>
/// Retrieves a <see cref="PropertyInfo"/> from a parameterless lambda function: a closure is actually required
/// and the compiler generates one automatically.
Expand All @@ -133,7 +86,7 @@ public static PropertyInfo GetPropertyInfo<TProperty>( Expression<Func<TProperty
return DoGetPropertyInfo( propertyLambda );
}

private static PropertyInfo DoGetPropertyInfo( LambdaExpression propertyLambda )
internal static PropertyInfo DoGetPropertyInfo( LambdaExpression propertyLambda )
{
Expression exp = propertyLambda.Body;
MemberExpression member = exp as MemberExpression;
Expand All @@ -148,25 +101,6 @@ private static PropertyInfo DoGetPropertyInfo( LambdaExpression propertyLambda )
return propInfo;
}

private static Action<THolder, TProperty> CreateSetter<THolder, TProperty>( PropertyInfo property, CreateInvalidSetterOption o )
{
var holderType = Expression.Parameter( typeof( THolder ), "e" );
var propType = Expression.Parameter( typeof( TProperty ), "v" );
MethodInfo s = property.GetSetMethod();
if( s == null )
{
if( o == CreateInvalidSetterOption.ThrowException ) throw new InvalidOperationException( string.Format( "Property '{0}' has no setter.", property.Name ) );
if( o == CreateInvalidSetterOption.NullAction ) return null;
return VoidAction;
}
return (Action<THolder, TProperty>)s.CreateDelegate( typeof( Action<THolder, TProperty> ) );
}

static void VoidAction<T1, T2>( T1 o1, T2 o2 )
{
}


/// <summary>
/// Creates an array of type of a method parameters.
/// </summary>
Expand Down Expand Up @@ -257,12 +191,12 @@ public static void GenerateCustomAttributeBuilder( IEnumerable<CustomAttributeDa
}
}

private static bool ConstructorSignatureMatch(ConstructorInfo c, IList<CustomAttributeTypedArgument> ctorArgs)
private static bool ConstructorSignatureMatch( ConstructorInfo c, IList<CustomAttributeTypedArgument> ctorArgs )
{
var ctorParameters = c.GetParameters().ToList();
if( ctorParameters.Count != ctorArgs.Count ) return false;
for (int i = 0; i < ctorParameters.Count; ++i )

for( int i = 0; i < ctorParameters.Count; ++i )
{
if( ctorParameters[i].ParameterType != ctorArgs[i].ArgumentType ) return false;
}
Expand Down
Loading

0 comments on commit 8441e0e

Please sign in to comment.