Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Telerik.JustMock/Core/MockingUtil.CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ public static Delegate CreateDynamicMethod(Type delegateType, Action<ILGenerator
// "The return Type contains some invalid type(i.e. null, ByRef)". In order to make
// it working we are applying the following simple workaround:
// 1. Pass a non-ref type to constructor and create dynamic method instance
// 2. Using reflection change the value of private field 'm_returnType' of the
// newly created dynamic method to be ByRef type
// 2. Using reflection change the value of the private field representing the
// return type of the newly created dynamic method to be ByRef type
Type dynamicReturnType = (returnType.IsByRef) ? returnType.GetElementType() : returnType;

var method = new DynamicMethod("DynamicMethod_" + Guid.NewGuid().ToString("N"), dynamicReturnType, parameterTypes, true);

if (returnType.IsByRef)
{
method.GetType().GetField("m_returnType", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
var returnTypeFieldName = Environment.Version.Major >= 8 ? "_returnType" : "m_returnType";
method.GetType().GetField(returnTypeFieldName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.SetValue(method, returnType);
}

Expand Down