Have you run the code in v17, you should not get any error.
Test works in Resharper test runner and MSTest using the NUnit Test Adapter, just does not work with NCrunch. 100% NCrunch issue.
Here is exception from NCrunch
System.Exception : Error registering message type List`1 with type.
----> System.Exception : Error creating WeakGetter for property Item on type System.Collections.Generic.List`1[TestName.Program+TestMessage2].
----> System.InvalidProgramException : Common Language Runtime detected an invalid program.
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 46
at TestName.Program.TestNotWorking() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 32
--Exception
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 97
at TestName.Program.Build(Type classType) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 62
at TestName.Program.RegisterMessageType[T]() in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 41
--InvalidProgramException
at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator)
at System.Linq.Expressions.Expression`1.Compile()
at TestName.Program.CreateWeakGetter(PropertyInfo propertyInfo) in c:\temp\ConsoleApp1\ConsoleApp1\Program.cs:line 93
here is code
Quote:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Net.Security;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using Nerdle.AutoConfig;
using NUnit.Framework;
namespace TestName
{
[TestFixture]
class Program
{
static void Main(string[] args)
{
}
private class TestMessage2
{
public DateTime DateTimeData { get; set; }
public string StringData { get; set; }
public int IntData { get; set; }
}
[Test]
public void TestNotWorking()
{
RegisterMessageType<List<TestMessage2>>();
}
private void RegisterMessageType<T>()
{
var messageType = typeof(T);
try
{
Build(messageType);
return;
}
catch (Exception ex)
{
throw new Exception($"Error registering message type {messageType.Name} with type.", ex);
}
}
private void Build(Type classType)
{
if (classType == null) throw new ArgumentNullException(nameof(classType));
// It is important that the schema stores the properties in a predictable order:
// a) as xml comparison tools used in unit tests are sensitive to field order.
// b) it makes log inspection etc much easier.
// For that reason they are sorted alphabetically regardless of definition order on the classes.
var propertyInfos = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var p in propertyInfos)
{
var weakGetter = CreateWeakGetter(p);
}
return;
}
/// <summary>
/// Create a weakly typed getter, i.e. one that takes an instance of type object as the argument and returns the boxed
/// result as an object.
/// </summary>
/// <param name="propertyInfo">PropertyInfo of the property to create a getter for.</param>
/// <returns>A compiled weakly typed property getter delegate.</returns>
private Func<object, object> CreateWeakGetter(PropertyInfo propertyInfo)
{
try
{
// We want to handle the class instance as an object so that we can work with a consistent delegate type across
// all mapping.
var parameter = Expression.Parameter(typeof(object));
// Convert the class instance argument from object into the declaring type.
// ReSharper disable once AssignNullToNotNullAttribute
var converted = Expression.Convert(parameter, propertyInfo.DeclaringType);
// Invoke the getter on the typed instance.
var property = Expression.Property(converted, propertyInfo);
// Box the result (convert to object).
var boxedResult = Expression.Convert(property, typeof(object));
// Build the delegate.
return Expression.Lambda<Func<object, object>>(boxedResult, parameter).Compile();
}
catch (Exception ex)
{
throw new Exception(
$"Error creating WeakGetter for property {propertyInfo.Name} on type {propertyInfo.ReflectedType}. ",
ex);
}
}
}
}