This commit is contained in:
Stephan
2018-10-16 10:58:17 +02:00
parent 6e8411acaa
commit f868172f48
4 changed files with 23 additions and 23 deletions

View File

@@ -71,7 +71,7 @@ namespace Umbraco.Core.Models.PublishedContent
throw new InvalidOperationException($"Both types {type.FullName} and {modelInfo.ModelType.FullName} want to be a model type for content type with alias \"{typeName}\".");
// have to use an unsafe ctor because we don't know the types, really
var modelCtor = ReflectionUtilities.EmitCtorUnsafe<Func<object, object>>(constructor);
var modelCtor = ReflectionUtilities.EmitConstructorUnsafe<Func<object, object>>(constructor);
modelInfos[typeName] = new ModelInfo { ParameterType = parameterType, ModelType = type, Ctor = modelCtor };
modelTypeMap[typeName] = type;
}
@@ -112,7 +112,7 @@ namespace Umbraco.Core.Models.PublishedContent
if (ctor != null) return ctor();
var listType = typeof(List<>).MakeGenericType(modelInfo.ModelType);
ctor = modelInfo.ListCtor = ReflectionUtilities.EmitCtor<Func<IList>>(declaring: listType);
ctor = modelInfo.ListCtor = ReflectionUtilities.EmitConstuctor<Func<IList>>(declaring: listType);
return ctor();
}

View File

@@ -295,7 +295,7 @@ namespace Umbraco.Core
/// <exception cref="InvalidOperationException">Occurs when the constructor does not exist and <paramref name="mustExist"/> is <c>true</c>.</exception>
/// <exception cref="ArgumentException">Occurs when <typeparamref name="TLambda"/> is not a Func or when <paramref name="declaring"/>
/// is specified and does not match the function's returned type.</exception>
public static TLambda EmitCtor<TLambda>(bool mustExist = true, Type declaring = null)
public static TLambda EmitConstuctor<TLambda>(bool mustExist = true, Type declaring = null)
{
var (_, lambdaParameters, lambdaReturned) = AnalyzeLambda<TLambda>(true, true);
@@ -313,7 +313,7 @@ namespace Umbraco.Core
}
// emit
return EmitCtorSafe<TLambda>(lambdaParameters, lambdaReturned, ctor);
return EmitConstructorSafe<TLambda>(lambdaParameters, lambdaReturned, ctor);
}
/// <summary>
@@ -325,16 +325,16 @@ namespace Umbraco.Core
/// <exception cref="ArgumentException">Occurs when <typeparamref name="TLambda"/> is not a Func or when its generic
/// arguments do not match those of <paramref name="ctor"/>.</exception>
/// <exception cref="ArgumentNullException">Occurs when <paramref name="ctor"/> is null.</exception>
public static TLambda EmitCtor<TLambda>(ConstructorInfo ctor)
public static TLambda EmitConstructor<TLambda>(ConstructorInfo ctor)
{
if (ctor == null) throw new ArgumentNullException(nameof(ctor));
var (_, lambdaParameters, lambdaReturned) = AnalyzeLambda<TLambda>(true, true);
return EmitCtorSafe<TLambda>(lambdaParameters, lambdaReturned, ctor);
return EmitConstructorSafe<TLambda>(lambdaParameters, lambdaReturned, ctor);
}
private static TLambda EmitCtorSafe<TLambda>(Type[] lambdaParameters, Type returned, ConstructorInfo ctor)
private static TLambda EmitConstructorSafe<TLambda>(Type[] lambdaParameters, Type returned, ConstructorInfo ctor)
{
// get type and args
var ctorDeclaring = ctor.DeclaringType;
@@ -350,7 +350,7 @@ namespace Umbraco.Core
ThrowInvalidLambda<TLambda>("ctor", ctorDeclaring, ctorParameters);
// emit
return EmitCtor<TLambda>(ctorDeclaring, ctorParameters, ctor);
return EmitConstructor<TLambda>(ctorDeclaring, ctorParameters, ctor);
}
/// <summary>
@@ -367,17 +367,17 @@ namespace Umbraco.Core
/// <exception cref="ArgumentException">Occurs when <typeparamref name="TLambda"/> is not a Func or when its generic
/// arguments do not match those of <paramref name="ctor"/>.</exception>
/// <exception cref="ArgumentNullException">Occurs when <paramref name="ctor"/> is null.</exception>
public static TLambda EmitCtorUnsafe<TLambda>(ConstructorInfo ctor)
public static TLambda EmitConstructorUnsafe<TLambda>(ConstructorInfo ctor)
{
if (ctor == null) throw new ArgumentNullException(nameof(ctor));
var (_, lambdaParameters, lambdaReturned) = AnalyzeLambda<TLambda>(true, true);
// emit - unsafe - use lambda's args and assume they are correct
return EmitCtor<TLambda>(lambdaReturned, lambdaParameters, ctor);
return EmitConstructor<TLambda>(lambdaReturned, lambdaParameters, ctor);
}
private static TLambda EmitCtor<TLambda>(Type declaring, Type[] lambdaParameters, ConstructorInfo ctor)
private static TLambda EmitConstructor<TLambda>(Type declaring, Type[] lambdaParameters, ConstructorInfo ctor)
{
// gets the method argument types
var ctorParameters = GetParameters(ctor);

View File

@@ -144,7 +144,7 @@ namespace Umbraco.Tests.Benchmarks
// however, unfortunately, the generated "compiled to delegate" code cannot access private stuff :(
_emittedCtor = ReflectionUtilities.EmitCtor<Func<IFoo, Foo>>();
_emittedCtor = ReflectionUtilities.EmitConstuctor<Func<IFoo, Foo>>();
}
public IFoo IlCtor(IFoo foo)

View File

@@ -13,16 +13,16 @@ namespace Umbraco.Tests.Clr
[Test]
public void EmitCtorEmits()
{
var ctor1 = ReflectionUtilities.EmitCtor<Func<Class1>>();
var ctor1 = ReflectionUtilities.EmitConstuctor<Func<Class1>>();
Assert.IsInstanceOf<Class1>(ctor1());
var ctor2 = ReflectionUtilities.EmitCtor<Func<object>>(declaring: typeof(Class1));
var ctor2 = ReflectionUtilities.EmitConstuctor<Func<object>>(declaring: typeof(Class1));
Assert.IsInstanceOf<Class1>(ctor2());
var ctor3 = ReflectionUtilities.EmitCtor<Func<int, Class3>>();
var ctor3 = ReflectionUtilities.EmitConstuctor<Func<int, Class3>>();
Assert.IsInstanceOf<Class3>(ctor3(42));
var ctor4 = ReflectionUtilities.EmitCtor<Func<int, object>>(declaring: typeof(Class3));
var ctor4 = ReflectionUtilities.EmitConstuctor<Func<int, object>>(declaring: typeof(Class3));
Assert.IsInstanceOf<Class3>(ctor4(42));
}
@@ -30,40 +30,40 @@ namespace Umbraco.Tests.Clr
public void EmitCtorEmitsFromInfo()
{
var ctorInfo = typeof(Class1).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, Array.Empty<Type>(), null);
var ctor1 = ReflectionUtilities.EmitCtor<Func<Class1>>(ctorInfo);
var ctor1 = ReflectionUtilities.EmitConstructor<Func<Class1>>(ctorInfo);
Assert.IsInstanceOf<Class1>(ctor1());
ctorInfo = typeof(Class1).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, new[] { typeof(int) }, null);
var ctor3 = ReflectionUtilities.EmitCtor<Func<int, object>>(ctorInfo);
var ctor3 = ReflectionUtilities.EmitConstructor<Func<int, object>>(ctorInfo);
Assert.IsInstanceOf<Class1>(ctor3(42));
Assert.Throws<ArgumentException>(() => ReflectionUtilities.EmitCtor<Func<string, object>>(ctorInfo));
Assert.Throws<ArgumentException>(() => ReflectionUtilities.EmitConstructor<Func<string, object>>(ctorInfo));
}
[Test]
public void EmitCtorEmitsPrivateCtor()
{
var ctor = ReflectionUtilities.EmitCtor<Func<string, Class3>>();
var ctor = ReflectionUtilities.EmitConstuctor<Func<string, Class3>>();
Assert.IsInstanceOf<Class3>(ctor("foo"));
}
[Test]
public void EmitCtorThrowsIfNotFound()
{
Assert.Throws<InvalidOperationException>(() => ReflectionUtilities.EmitCtor<Func<bool, Class3>>());
Assert.Throws<InvalidOperationException>(() => ReflectionUtilities.EmitConstuctor<Func<bool, Class3>>());
}
[Test]
public void EmitCtorThrowsIfInvalid()
{
var ctorInfo = typeof(Class1).GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, Array.Empty<Type>(), null);
Assert.Throws<ArgumentException>(() => ReflectionUtilities.EmitCtor<Func<Class2>>(ctorInfo));
Assert.Throws<ArgumentException>(() => ReflectionUtilities.EmitConstructor<Func<Class2>>(ctorInfo));
}
[Test]
public void EmitCtorReturnsNull()
{
Assert.IsNull(ReflectionUtilities.EmitCtor<Func<bool, Class3>>(false));
Assert.IsNull(ReflectionUtilities.EmitConstuctor<Func<bool, Class3>>(false));
}
[Test]