static void FillLuaInvokerGenericArguments(MethodDefinition prototypeMethod, bool bIgnoreReturnValue, bool bAppendCoroutineState, ref MethodReference invoker) { if (invoker.HasGenericParameters) { GenericInstanceMethod genericInjectMethod = new GenericInstanceMethod(invoker.CloneMethod()); if (prototypeMethod.HasThis) { genericInjectMethod.GenericArguments.Add(prototypeMethod.DeclaringType); } foreach (ParameterDefinition parameter in prototypeMethod.Parameters) { var paramType = parameter.ParameterType.IsByReference ? ElementType.For(parameter.ParameterType) : parameter.ParameterType; genericInjectMethod.GenericArguments.Add(paramType); } if (bAppendCoroutineState) { genericInjectMethod.GenericArguments.Add(intTypeRef); } if (prototypeMethod.GotPassedByReferenceParam()) { genericInjectMethod.GenericArguments.Add(luaTableTypeDef); } else if (!bIgnoreReturnValue && !prototypeMethod.ReturnVoid()) { genericInjectMethod.GenericArguments.Add(prototypeMethod.ReturnType); } invoker = genericInjectMethod; } }
public static string GetTypeName(TypeReference t, bool bFull = false) { if (t.IsArray) { string str = GetTypeName(ElementType.For(t)); str += GetArrayRank(t); return(str); } else if (t.IsByReference) { //t = t.GetElementType(); return(GetTypeName(ElementType.For(t)) + "&"); } else if (t.IsGenericInstance) { return(GetGenericName(t, bFull)); } else if (t.MetadataType == MetadataType.Void) { return("void"); } else { string name = GetPrimitiveTypeStr(t, bFull); return(name.Replace('+', '.')); } }
static void ParseArgumentReference(MethodDefinition target, Instruction endPoint, int paramIndex) { ParameterDefinition param = null; ILProcessor il = target.Body.GetILProcessor(); if (target.HasThis) { if (paramIndex > 0) { param = target.Parameters[paramIndex - 1]; } else if (target.DeclaringType.IsValueType) { il.InsertBefore(endPoint, il.Create(OpCodes.Ldobj, target.DeclaringType)); } } else if (!target.HasThis) { param = target.Parameters[paramIndex]; } if (param != null && param.ParameterType.IsByReference) { TypeReference paramType = ElementType.For(param.ParameterType); if (paramType.IsValueType) { il.InsertBefore(endPoint, il.Create(OpCodes.Ldobj, paramType)); } else { il.InsertBefore(endPoint, il.Create(OpCodes.Ldind_Ref)); } } }
public static bool IsGenericTypeDefinition(this TypeReference type) { if (type.HasGenericParameters) { return(true); } else if (type.IsByReference || type.IsArray) { return(ElementType.For(type).IsGenericTypeDefinition()); } else if (type.IsNested) { var parent = type.DeclaringType; while (parent != null) { if (parent.IsGenericTypeDefinition()) { return(true); } if (parent.IsNested) { parent = parent.DeclaringType; } else { break; } } } return(type.IsGenericParameter); }
public static bool IsAssignableTo(this TypeReference typeRef, string typeName) { bool result; try { if (typeRef.IsGenericInstance) { result = ElementType.For(typeRef).IsAssignableTo(typeName); } else if (typeRef.FullName == typeName) { result = true; } else { result = typeRef.CheckedResolve().IsSubclassOf(typeName); } } catch (AssemblyResolutionException) { result = false; } return(result); }
public static bool IsAssignableTo(this TypeReference typeRef, string typeName) { try { if (typeRef.IsGenericInstance) { return(ElementType.For(typeRef).IsAssignableTo(typeName)); } return((typeRef.FullName == typeName) || typeRef.CheckedResolve().IsSubclassOf(typeName)); } catch (AssemblyResolutionException) { return(false); } }
static void UpdatePassedByReferenceParams(MethodDefinition target, bool bConfirmPopReturnValue) { if (!target.GotPassedByReferenceParam()) { return; } int updateCount = 0; ILProcessor il = target.Body.GetILProcessor(); VariableDefinition luaTable = GetResultTable(target); var rawGetGenericMethod = luaTableTypeDef.Methods.Single(method => method.Name == "RawGetIndex"); foreach (var param in target.Parameters) { if (!param.ParameterType.IsByReference) { continue; } var paramType = ElementType.For(param.ParameterType); il.InsertBefore(cursor, il.Create(OpCodes.Ldarg, param)); il.InsertBefore(cursor, il.Create(OpCodes.Ldloc, luaTable)); il.InsertBefore(cursor, il.Create(OpCodes.Ldc_I4, ++updateCount)); il.InsertBefore(cursor, il.Create(OpCodes.Call, rawGetGenericMethod.MakeGenericMethod(paramType))); if (paramType.IsValueType) { il.InsertBefore(cursor, il.Create(OpCodes.Stobj, paramType)); } else { il.InsertBefore(cursor, il.Create(OpCodes.Stind_Ref)); } } if (!bConfirmPopReturnValue && !target.ReturnVoid()) { il.InsertBefore(cursor, il.Create(OpCodes.Ldloc, luaTable)); il.InsertBefore(cursor, il.Create(OpCodes.Ldc_I4, ++updateCount)); il.InsertBefore(cursor, il.Create(OpCodes.Call, rawGetGenericMethod.MakeGenericMethod(target.ReturnType))); } }
public static bool IsAssignableTo(this TypeReference typeRef, string typeName) { try { if (typeRef.IsGenericInstance) { return(ElementType.For(typeRef).IsAssignableTo(typeName)); } if (typeRef.FullName == typeName) { return(true); } return(typeRef.CheckedResolve().IsSubclassOf(typeName)); } catch (AssemblyResolutionException) // If we can't resolve our typeref or one of its base types, { // let's assume it is not assignable to our target type return(false); } }