public override string GetNewObjectExpression(DmdConstructorInfo ctor, string argumentExpression, DmdType expectedType) { argumentExpression = LanguageValueNodeFactory.RemoveFormatSpecifiers(argumentExpression); var sb = ObjectCache.AllocStringBuilder(); var output = new StringBuilderTextColorOutput(sb); output.Write(BoxedTextColor.Keyword, "New"); output.Write(BoxedTextColor.Text, " "); FormatTypeName(output, ctor.DeclaringType); output.Write(BoxedTextColor.Punctuation, "("); var castType = ctor.GetMethodSignature().GetParameterTypes()[0]; bool needCast = !expectedType.CanCastTo(castType); if (needCast) { output.Write(BoxedTextColor.Keyword, "CType"); output.Write(BoxedTextColor.Punctuation, "("); } output.Write(BoxedTextColor.Text, argumentExpression); if (needCast) { output.Write(BoxedTextColor.Punctuation, ","); output.WriteSpace(); new Formatters.VisualBasic.VisualBasicTypeFormatter(new StringBuilderTextColorOutput(sb), VisualBasicValueNodeFactory.TypeFormatterOptions, null).Format(castType, null); output.Write(BoxedTextColor.Punctuation, ")"); } output.Write(BoxedTextColor.Punctuation, ")"); return(ObjectCache.FreeAndToString(ref sb)); }
public override ILValue CreateInstance(DmdConstructorInfo ctor, ILValue[] arguments) { DbgDotNetValueResult res; DbgDotNetArrayDimensionInfo[] dimensionInfos; switch (ctor.SpecialMethodKind) { case DmdSpecialMethodKind.Array_Constructor1: dimensionInfos = new DbgDotNetArrayDimensionInfo[arguments.Length]; for (int i = 0; i < dimensionInfos.Length; i++) { dimensionInfos[i] = new DbgDotNetArrayDimensionInfo(0, (uint)ReadInt32(arguments[i])); } res = runtime.CreateArray(context, frame, ctor.ReflectedType.GetElementType(), dimensionInfos, cancellationToken); return(CreateILValue(res)); case DmdSpecialMethodKind.Array_Constructor2: dimensionInfos = new DbgDotNetArrayDimensionInfo[arguments.Length / 2]; for (int i = 0; i < dimensionInfos.Length; i++) { dimensionInfos[i] = new DbgDotNetArrayDimensionInfo(ReadInt32(arguments[i * 2]), (uint)ReadInt32(arguments[i * 2 + 1])); } res = runtime.CreateArray(context, frame, ctor.ReflectedType.GetElementType(), dimensionInfos, cancellationToken); return(CreateILValue(res)); default: res = runtime.CreateInstance(context, frame, ctor, Convert(arguments, ctor.GetMethodSignature().GetParameterTypes()), cancellationToken); return(CreateILValue(res)); } }
DbgDotNetValueResult CreateInstanceCore(DmdConstructorInfo ctor, ILValue[] arguments) { if (ctor.IsStatic) return new DbgDotNetValueResult(PredefinedEvaluationErrorMessages.InternalDebuggerError); const DotNetClassHookCallOptions options = DotNetClassHookCallOptions.None; foreach (var anyHook in anyClassHooks) { var res = anyHook.CreateInstance(options, ctor, arguments); if (res != null) return new DbgDotNetValueResult(res, valueIsException: false); } var type = ctor.DeclaringType; if (type.IsConstructedGenericType) type = type.GetGenericTypeDefinition(); var typeName = DmdTypeName.Create(type); if (classHooks.TryGetValue(typeName, out var hook)) { if (DmdWellKnownTypeUtils.TryGetWellKnownType(typeName, out var wellKnownType)) { if (type != type.AppDomain.GetWellKnownType(wellKnownType, isOptional: true)) hook = null; } if (hook != null) { var res = hook.CreateInstance(options, ctor, arguments); if (res != null) return new DbgDotNetValueResult(res, valueIsException: false); } } if (!canFuncEval) throw new InterpreterMessageException(PredefinedEvaluationErrorMessages.FuncEvalDisabled); return runtime.CreateInstance(context, frame, ctor, Convert(arguments, ctor.GetMethodSignature().GetParameterTypes()), cancellationToken); }
DmdCustomAttributeData Read(DmdConstructorInfo ctor) { var ctorParams = ctor.GetMethodSignature().GetParameterTypes(); bool isEmpty = ctorParams.Count == 0 && reader.Position == reader.Length; if (!isEmpty && reader.ReadUInt16() != 1) { throw new CABlobParserException("Invalid CA blob prolog"); } var ctorArgs = new DmdCustomAttributeTypedArgument[ctorParams.Count]; for (int i = 0; i < ctorArgs.Length; i++) { ctorArgs[i] = ReadFixedArg(FixTypeSig(ctorParams[i])); } // Some tools don't write the next ushort if there are no named arguments. int numNamedArgs = reader.Position == reader.Length ? 0 : reader.ReadUInt16(); var namedArgs = ReadNamedArguments(numNamedArgs); // Match reflection named argument order: fields before properties, // and order of fields/properties is identical to the order returned // by GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) and // GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic). Array.Sort(namedArgs, ReflectionNamedArgumentComparerDelegate); return(new DmdCustomAttributeData(ctor, ctorArgs, namedArgs, isPseudoCustomAttribute: false)); }
public override ILValue CreateInstance(DmdConstructorInfo ctor, ILValue[] arguments) { DbgDotNetValueResult res; DbgDotNetArrayDimensionInfo[] dimensionInfos; switch (ctor.SpecialMethodKind) { case DmdSpecialMethodKind.Array_Constructor1: dimensionInfos = new DbgDotNetArrayDimensionInfo[arguments.Length]; for (int i = 0; i < dimensionInfos.Length; i++) { dimensionInfos[i] = new DbgDotNetArrayDimensionInfo(0, (uint)ReadInt32(arguments[i])); } res = runtime.CreateArray(evalInfo, ctor.ReflectedType.GetElementType(), dimensionInfos); return(CreateILValue(res)); case DmdSpecialMethodKind.Array_Constructor2: dimensionInfos = new DbgDotNetArrayDimensionInfo[arguments.Length / 2]; for (int i = 0; i < dimensionInfos.Length; i++) { dimensionInfos[i] = new DbgDotNetArrayDimensionInfo(ReadInt32(arguments[i * 2]), (uint)ReadInt32(arguments[i * 2 + 1])); } res = runtime.CreateArray(evalInfo, ctor.ReflectedType.GetElementType(), dimensionInfos); return(CreateILValue(res)); default: res = CreateInstanceCore(ctor, arguments); return(CreateILValue(res)); } }
public OptionalAttributeInfo(DmdParameterInfo parameter) { if (parameter.IsOptional) { var caType = parameter.Member.AppDomain.GetWellKnownType(DmdWellKnownType.System_Runtime_InteropServices_OptionalAttribute, isOptional: true); ctor = caType?.GetConstructor(Array.Empty <DmdType>()); Debug.Assert((object)caType == null || (object)ctor != null); } else { ctor = null; } }
protected override string GetNewObjectExpression(DmdConstructorInfo ctor, string argumentExpression) { var sb = Formatters.ObjectCache.AllocStringBuilder(); var output = new StringBuilderTextColorOutput(sb); output.Write(BoxedTextColor.Keyword, "New"); output.Write(BoxedTextColor.Text, " "); FormatTypeName(output, ctor.DeclaringType); output.Write(BoxedTextColor.Punctuation, "("); output.Write(BoxedTextColor.Text, argumentExpression); output.Write(BoxedTextColor.Punctuation, ")"); return(Formatters.ObjectCache.FreeAndToString(ref sb)); }
public PreserveSigAttributeInfo(DmdMethodInfo method) { if ((method.MethodImplementationFlags & DmdMethodImplAttributes.PreserveSig) != 0) { var caType = method.AppDomain.GetWellKnownType(DmdWellKnownType.System_Runtime_InteropServices_PreserveSigAttribute, isOptional: true); ctor = caType?.GetConstructor(Array.Empty <DmdType>()); Debug.Assert((object)caType == null || (object)ctor != null); } else { ctor = null; } }
public static DmdCustomAttributeData Read(DmdModule module, DmdDataStream stream, DmdConstructorInfo ctor) { using (var reader = new DmdCustomAttributeReader(module, stream, ctor.ReflectedType, GetGenericArguments(ctor.ReflectedType), ownsReader: true)) { try { return reader.Read(ctor); } catch (CABlobParserException) { } catch (ResolveException) { } catch (IOException) { } return null; } }
static (DmdConstructorInfo ctor, IList <DmdCustomAttributeTypedArgument> constructorArguments) GetConstructor(DmdType type, SecurityAction action) { var appDomain = type.AppDomain; var securityActionType = appDomain.GetWellKnownType(DmdWellKnownType.System_Security_Permissions_SecurityAction); var ctor = type.GetConstructor(new[] { securityActionType }); if ((object)ctor != null) { var ctorArgs = new[] { new DmdCustomAttributeTypedArgument(securityActionType, (int)action) }; return(ctor, ctorArgs); } ctor = type.GetConstructor(Array.Empty <DmdType>()) ?? type.GetConstructors().FirstOrDefault(); return(ctor, null); }
public override string GetNewObjectExpression(DmdConstructorInfo ctor, string argumentExpression, DmdType expectedType) { argumentExpression = LanguageValueNodeFactory.RemoveFormatSpecifiers(argumentExpression); var sb = ObjectCache.AllocStringBuilder(); var output = new DbgStringBuilderTextWriter(sb); output.Write(DbgTextColor.Keyword, "new"); output.Write(DbgTextColor.Text, " "); FormatTypeName(output, ctor.DeclaringType); output.Write(DbgTextColor.Punctuation, "("); var castType = ctor.GetMethodSignature().GetParameterTypes()[0]; if (!expectedType.CanCastTo(castType)) { output.Write(DbgTextColor.Punctuation, "("); new Formatters.CSharp.CSharpTypeFormatter(new DbgStringBuilderTextWriter(sb), CSharpValueNodeFactory.TypeFormatterOptions, null).Format(castType, null); output.Write(DbgTextColor.Punctuation, ")"); } output.Write(DbgTextColor.Text, argumentExpression); output.Write(DbgTextColor.Punctuation, ")"); return(ObjectCache.FreeAndToString(ref sb)); }
/// <summary> /// Creates a new instance and calls its constructor or returns null on failure. The constructor could be a CLR-generated array constructor /// </summary> /// <param name="ctor">Constructor</param> /// <param name="arguments">Constructor arguments</param> /// <returns></returns> public abstract ILValue CreateInstance(DmdConstructorInfo ctor, ILValue[] arguments);
public virtual DbgDotNetValue CreateInstance(DotNetClassHookCallOptions options, DmdConstructorInfo ctor, ILValue[] arguments) => null;
public abstract string GetNewObjectExpression(DmdConstructorInfo ctor, string argumentExpression, DmdType expectedType);
public override DbgDotNetValue?CreateInstance(DmdConstructorInfo ctor, object?[] arguments) { var appDomain = ctor.AppDomain; var ps = ctor.GetMethodSignature().GetParameterTypes(); ValueLocation valueLocation; switch (ps.Count) { case 1: // String(char* value) if (ps[0].IsPointer && ps[0].GetElementType() == appDomain.System_Char) { //TODO: break; } // String(sbyte* value) if (ps[0].IsPointer && ps[0].GetElementType() == appDomain.System_SByte) { //TODO: break; } // String(char[] value) if (ps[0].IsSZArray && ps[0].GetElementType() == appDomain.System_Char) { var value = runtime.ValueConverter.ToCharArray(arguments[0]); var s = new string(value); valueLocation = new NoValueLocation(appDomain.System_String, MonoValueFactory.TryCreateSyntheticValue(engine, GetMonoAppDomain(ctor), appDomain.System_String, s)); return(engine.CreateDotNetValue_MonoDebug(valueLocation)); } break; case 2: // String(char c, int count) if (ps[0] == appDomain.System_Char && ps[1] == appDomain.System_Int32) { char c = runtime.ValueConverter.ToChar(arguments[0]); int count = runtime.ValueConverter.ToInt32(arguments[1]); var s = new string(c, count); valueLocation = new NoValueLocation(appDomain.System_String, MonoValueFactory.TryCreateSyntheticValue(engine, GetMonoAppDomain(ctor), appDomain.System_String, s)); return(engine.CreateDotNetValue_MonoDebug(valueLocation)); } break; case 3: // String(char* value, int startIndex, int length) if (ps[0].IsPointer && ps[0].GetElementType() == appDomain.System_Char && ps[1] == appDomain.System_Int32 && ps[2] == appDomain.System_Int32) { //TODO: break; } // String(sbyte* value, int startIndex, int length) if (ps[0].IsPointer && ps[0].GetElementType() == appDomain.System_SByte && ps[1] == appDomain.System_Int32 && ps[2] == appDomain.System_Int32) { //TODO: break; } // String(char[] value, int startIndex, int length) if (ps[0].IsSZArray && ps[0].GetElementType() == appDomain.System_Char && ps[1] == appDomain.System_Int32 && ps[2] == appDomain.System_Int32) { var value = runtime.ValueConverter.ToCharArray(arguments[0]); int startIndex = runtime.ValueConverter.ToInt32(arguments[1]); int length = runtime.ValueConverter.ToInt32(arguments[2]); var s = new string(value, startIndex, length); valueLocation = new NoValueLocation(appDomain.System_String, MonoValueFactory.TryCreateSyntheticValue(engine, GetMonoAppDomain(ctor), appDomain.System_String, s)); return(engine.CreateDotNetValue_MonoDebug(valueLocation)); } break; case 4: // String(sbyte* value, int startIndex, int length, Encoding enc) if (ps[0].IsPointer && ps[0].GetElementType() == appDomain.System_SByte && ps[1] == appDomain.System_Int32 && ps[2] == appDomain.System_Int32 && ps[3] == appDomain.GetWellKnownType(DmdWellKnownType.System_Text_Encoding, isOptional: true)) { //TODO: break; } break; } return(null); }
public virtual DbgDotNetValue?CreateInstance(DmdConstructorInfo ctor, object?[] arguments) => null;
protected abstract string GetNewObjectExpression(DmdConstructorInfo ctor, string argumentExpression);
/// <summary> /// Creates a new instance of a type /// </summary> /// <param name="context">Evaluation context</param> /// <param name="ctor">Constructor</param> /// <param name="arguments">Arguments passed to the constructor</param> /// <returns></returns> public abstract object CreateInstance(object context, DmdConstructorInfo ctor, object[] arguments);
public ProxyState(DmdConstructorInfo constructor) => Constructor = constructor;