Esempio n. 1
0
        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));
        }
            static bool CheckIsDynamicViewType(DmdType type)
            {
                // Windows Runtime types aren't supported
                if (type.IsWindowsRuntime)
                {
                    return(false);
                }

                // Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView supports COM objects
                if (type.CanCastTo(type.AppDomain.GetWellKnownType(DmdWellKnownType.System___ComObject, isOptional: true)))
                {
                    return(true);
                }

                // Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView supports IDynamicMetaObjectProvider.
                // This type is defined in Microsoft.CSharp which isn't always loaded. That's the reason we don't
                // use a DmdWellKnownType, since when searching for one, the code will check every loaded assembly
                // until the type is found, forcing all lazy-loaded metadata to be loaded. Most of the time it would
                // fail, and thus load all metadata. It's a problem when debugging programs with 100+ loaded assemblies.
                foreach (var iface in type.GetInterfaces())
                {
                    if (iface.DeclaringType is null && iface.MetadataNamespace == "System.Dynamic" && iface.MetadataName == "IDynamicMetaObjectProvider")
                    {
                        return(true);
                    }
                }

                return(false);
            }
 protected static bool NeedCast(DmdType slotType, DmdType memberDeclaringType)
 {
     if (slotType.IsInterface)
     {
         return(true);
     }
     else
     {
         return(!slotType.CanCastTo(memberDeclaringType));
     }
 }
Esempio n. 4
0
        DisplayPart[] GetDisplayParts(DmdType type)
        {
            var ddaType = type.AppDomain.GetWellKnownType(DmdWellKnownType.System_Diagnostics_DebuggerDisplayAttribute, isOptional: true);

            Debug.Assert((object)ddaType != null);
            if ((object)ddaType == null)
            {
                return(null);
            }

            string debuggerDisplayString;
            var    attr = type.FindCustomAttribute(ddaType, inherit: true);

            if (attr == null)
            {
                if (type.CanCastTo(type.AppDomain.System_Type))
                {
                    // Show the same thing VS shows
                    debuggerDisplayString = @"\{Name = {Name} FullName = {FullName}\}";
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                if (attr.ConstructorArguments.Count == 1)
                {
                    debuggerDisplayString = attr.ConstructorArguments[0].Value as string;
                }
                else
                {
                    debuggerDisplayString = null;
                }
            }
            if (string.IsNullOrEmpty(debuggerDisplayString))
            {
                return(null);
            }

            return(CreateDisplayParts(debuggerDisplayString));
        }
Esempio n. 5
0
        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));
        }
        (DisplayPart[] nameParts, DisplayPart[] valueParts, DisplayPart[] typeParts) GetDisplayParts(DmdType type)
        {
            var ddaType = type.AppDomain.GetWellKnownType(DmdWellKnownType.System_Diagnostics_DebuggerDisplayAttribute, isOptional: true);

            Debug.Assert((object)ddaType != null);

            // We have special support for formatting KeyValuePair<K, V>, so ignore all DebuggerDisplayAttributes.
            // (Only Unity and older Mono versions have a DebuggerDisplayAttribute on it)
            bool   forceNoAttr = type.IsConstructedGenericType && type.GetGenericTypeDefinition() == type.AppDomain.GetWellKnownType(DmdWellKnownType.System_Collections_Generic_KeyValuePair_T2, isOptional: true);
            string nameDisplayString = null, valueDisplayString = null, typeDisplayString = null;

            if (!forceNoAttr && (object)ddaType != null)
            {
                var attr = type.FindCustomAttribute(ddaType, inherit: true);
                if (attr == null)
                {
                    if (type.CanCastTo(type.AppDomain.System_Type))
                    {
                        // Show the same thing VS shows
                        valueDisplayString = @"\{Name = {Name} FullName = {FullName}\}";
                    }
                }
                else
                {
                    if (attr.ConstructorArguments.Count == 1)
                    {
                        valueDisplayString = attr.ConstructorArguments[0].Value as string;
                    }
                    nameDisplayString = GetString(attr, nameof(DebuggerDisplayAttribute.Name));
                    typeDisplayString = GetString(attr, nameof(DebuggerDisplayAttribute.Type));
                }
            }

            var nameParts  = CreateDisplayParts(nameDisplayString);
            var valueParts = CreateDisplayParts(valueDisplayString);
            var typeParts  = CreateDisplayParts(typeDisplayString);

            return(nameParts, valueParts, typeParts);
        }
        (DisplayPart[] nameParts, DisplayPart[] valueParts, DisplayPart[] typeParts) GetDisplayParts(DmdType type)
        {
            var ddaType = type.AppDomain.GetWellKnownType(DmdWellKnownType.System_Diagnostics_DebuggerDisplayAttribute, isOptional: true);

            Debug2.Assert(ddaType is not null);

            bool   forceNoAttr = ShouldIgnoreDebuggerDisplayAttribute(type);
            string?nameDisplayString = null, valueDisplayString = null, typeDisplayString = null;

            if (!forceNoAttr && ddaType is not null)
            {
                var attr = type.FindCustomAttribute(ddaType, inherit: true);
                if (attr is null)
                {
                    if (type.CanCastTo(type.AppDomain.System_Type))
                    {
                        // Show the same thing VS shows
                        valueDisplayString = @"\{Name = {Name} FullName = {FullName}\}";
                    }
                }
                else
                {
                    if (attr.ConstructorArguments.Count == 1)
                    {
                        valueDisplayString = attr.ConstructorArguments[0].Value as string;
                    }
                    nameDisplayString = GetString(attr, nameof(DebuggerDisplayAttribute.Name));
                    typeDisplayString = GetString(attr, nameof(DebuggerDisplayAttribute.Type));
                }
            }

            var nameParts  = CreateDisplayParts(nameDisplayString);
            var valueParts = CreateDisplayParts(valueDisplayString);
            var typeParts  = CreateDisplayParts(typeDisplayString);

            return(nameParts, valueParts, typeParts);
        }