ConvertString() public static méthode

Converts special characters to escape sequences within the given string.
public static ConvertString ( string str ) : string
str string
Résultat string
Exemple #1
0
        private void WriteSecurityDeclarationArgument(CustomAttributeNamedArgument na)
        {
            var type = na.Argument.Type;

            if (type.MetadataType == MetadataType.Class || type.MetadataType == MetadataType.ValueType)
            {
                _o.Write("enum ");
                if (type.Scope != type.Module)
                {
                    _o.Write("class ");
                    _o.Write(DisassemblerHelpers.Escape(GetAssemblyQualifiedName(type)));
                }
                else
                {
                    type.WriteTo(_o, IlNameSyntax.TypeName);
                }
            }
            else
            {
                type.WriteTo(_o);
            }

            _o.Write(' ');
            _o.Write(DisassemblerHelpers.Escape(na.Name));
            _o.Write(" = ");

            var s = na.Argument.Value as string;

            if (s != null)
            {
                // secdecls use special syntax for strings
                _o.Write("string('{0}')", DisassemblerHelpers.ConvertString(s).Replace("'", "\'"));
            }
            else
            {
                WriteConstant(na.Argument.Value);
            }
        }
Exemple #2
0
        private void WriteNativeType(NativeType nativeType, MarshalInfo marshalInfo = null)
        {
            //ncrunch: no coverage start
            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (nativeType)
            {
            case NativeType.None: break;

            case NativeType.Boolean: _o.Write("bool"); break;

            case NativeType.I1: _o.Write("int8"); break;

            case NativeType.U1: _o.Write("unsigned int8"); break;

            case NativeType.I2: _o.Write("int16"); break;

            case NativeType.U2: _o.Write("unsigned int16"); break;

            case NativeType.I4: _o.Write("int32"); break;

            case NativeType.U4: _o.Write("unsigned int32"); break;

            case NativeType.I8: _o.Write("int64"); break;

            case NativeType.U8: _o.Write("unsigned int64"); break;

            case NativeType.R4: _o.Write("float32"); break;

            case NativeType.R8: _o.Write("float64"); break;

            case NativeType.LPStr: _o.Write("lpstr"); break;

            case NativeType.Int: _o.Write("int"); break;

            case NativeType.UInt: _o.Write("unsigned int"); break;

            case NativeType.Func: goto default;                     // ??

            case NativeType.Array: var ami = (ArrayMarshalInfo)marshalInfo;
                if (ami == null)
                {
                    goto default;
                }
                if (ami.ElementType != NativeType.Max)
                {
                    WriteNativeType(ami.ElementType);
                }
                _o.Write('[');
                if (ami.SizeParameterMultiplier == 0)
                {
                    _o.Write(ami.Size.ToString());
                }
                else
                {
                    if (ami.Size >= 0)
                    {
                        _o.Write(ami.Size.ToString());
                    }
                    _o.Write(" + ");
                    _o.Write(ami.SizeParameterIndex.ToString());
                }
                _o.Write(']'); break;

            case NativeType.Currency: _o.Write("currency"); break;

            case NativeType.BStr: _o.Write("bstr"); break;

            case NativeType.LPWStr: _o.Write("lpwstr"); break;

            case NativeType.LPTStr: _o.Write("lptstr"); break;

            // ReSharper disable once PossibleNullReferenceException
            case NativeType.FixedSysString: _o.Write("fixed sysstring[{0}]", ((FixedSysStringMarshalInfo)marshalInfo).Size); break;

            case NativeType.IUnknown: _o.Write("iunknown"); break;

            case NativeType.IDispatch: _o.Write("idispatch"); break;

            case NativeType.Struct: _o.Write("struct"); break;

            case NativeType.IntF: _o.Write("interface"); break;

            case NativeType.SafeArray: _o.Write("safearray "); var sami = marshalInfo as SafeArrayMarshalInfo;
                if (sami != null)
                {
                    switch (sami.ElementType)
                    {
                    case VariantType.None: break;

                    case VariantType.I2: _o.Write("int16"); break;

                    case VariantType.I4: _o.Write("int32"); break;

                    case VariantType.R4: _o.Write("float32"); break;

                    case VariantType.R8: _o.Write("float64"); break;

                    case VariantType.CY: _o.Write("currency"); break;

                    case VariantType.Date: _o.Write("date"); break;

                    case VariantType.BStr: _o.Write("bstr"); break;

                    case VariantType.Dispatch: _o.Write("idispatch"); break;

                    case VariantType.Error: _o.Write("error"); break;

                    case VariantType.Bool: _o.Write("bool"); break;

                    case VariantType.Variant: _o.Write("variant"); break;

                    case VariantType.Unknown: _o.Write("iunknown"); break;

                    case VariantType.Decimal: _o.Write("decimal"); break;

                    case VariantType.I1: _o.Write("int8"); break;

                    case VariantType.UI1: _o.Write("unsigned int8"); break;

                    case VariantType.UI2: _o.Write("unsigned int16"); break;

                    case VariantType.UI4: _o.Write("unsigned int32"); break;

                    case VariantType.Int: _o.Write("int"); break;

                    case VariantType.UInt: _o.Write("unsigned int"); break;

                    default: _o.Write(sami.ElementType.ToString()); break;
                    }
                }
                break;

            case NativeType.FixedArray: _o.Write("fixed array");
                var fami = marshalInfo as FixedArrayMarshalInfo;
                if (fami != null)
                {
                    _o.Write("[{0}]", fami.Size);
                    if (fami.ElementType != NativeType.None)
                    {
                        _o.Write(' ');
                    }
                    WriteNativeType(fami.ElementType);
                }
                break;

            case NativeType.ByValStr: _o.Write("byvalstr"); break;

            case NativeType.ANSIBStr: _o.Write("ansi bstr"); break;

            case NativeType.TBStr: _o.Write("tbstr"); break;

            case NativeType.VariantBool: _o.Write("variant bool"); break;

            case NativeType.ASAny: _o.Write("as any"); break;

            case NativeType.LPStruct: _o.Write("lpstruct"); break;

            case NativeType.CustomMarshaler: var cmi = marshalInfo as CustomMarshalInfo; if (cmi == null)
                {
                    goto default;
                }
                _o.Write("custom(\"{0}\", \"{1}\"", DisassemblerHelpers.ConvertString(cmi.ManagedType.FullName), DisassemblerHelpers.ConvertString(cmi.Cookie));
                if (cmi.Guid != Guid.Empty || !string.IsNullOrEmpty(cmi.UnmanagedType))
                {
                    _o.Write(", \"{0}\", \"{1}\"", cmi.Guid.ToString(), DisassemblerHelpers.ConvertString(cmi.UnmanagedType));
                }
                _o.Write(')'); break;

            case NativeType.Error: _o.Write("error"); break;

            default: _o.Write(nativeType.ToString()); break;
            }
            //ncrunch: no coverage end
        }
Exemple #3
0
        private void DisassembleMethodInternal(MethodDefinition method)
        {
            //    .method public hidebysig  specialname
            //               instance default class [mscorlib]System.IO.TextWriter get_BaseWriter ()  cil managed
            //

            //emit flags
            WriteEnum(method.Attributes & MethodAttributes.MemberAccessMask, _methodVisibility);
            WriteFlags(method.Attributes & ~MethodAttributes.MemberAccessMask, _methodAttributeFlags);
            if (method.IsCompilerControlled)
            {
                _o.Write("privatescope ");
            }

            if ((method.Attributes & MethodAttributes.PInvokeImpl) == MethodAttributes.PInvokeImpl)
            {
                _o.Write("pinvokeimpl");
                if (method.HasPInvokeInfo && method.PInvokeInfo != null)
                {
                    var info = method.PInvokeInfo;
                    _o.Write("(\"" + DisassemblerHelpers.ConvertString(info.Module.Name) + "\"");

                    if (!string.IsNullOrEmpty(info.EntryPoint) && info.EntryPoint != method.Name)
                    {
                        _o.Write(" as \"" + DisassemblerHelpers.ConvertString(info.EntryPoint) + "\"");
                    }

                    if (info.IsNoMangle)
                    {
                        _o.Write(" nomangle");
                    }

                    if (info.IsCharSetAnsi)
                    {
                        _o.Write(" ansi");
                    }
                    else if (info.IsCharSetAuto)
                    {
                        _o.Write(" autochar");
                    }
                    else if (info.IsCharSetUnicode)
                    {
                        _o.Write(" unicode");
                    }

                    if (info.SupportsLastError)
                    {
                        _o.Write(" lasterr");
                    }

                    if (info.IsCallConvCdecl)
                    {
                        _o.Write(" cdecl");
                    }
                    else if (info.IsCallConvFastcall)
                    {
                        _o.Write(" fastcall");
                    }
                    else if (info.IsCallConvStdCall)
                    {
                        _o.Write(" stdcall");
                    }
                    else if (info.IsCallConvThiscall)
                    {
                        _o.Write(" thiscall");
                    }
                    else if (info.IsCallConvWinapi)
                    {
                        _o.Write(" winapi");
                    }

                    _o.Write(')');
                }
                _o.Write(' ');
            }

            _o.WriteLine();
            _o.Indent();
            if (method.ExplicitThis)
            {
                _o.Write("instance explicit ");
            }
            else if (method.HasThis)
            {
                _o.Write("instance ");
            }

            //call convention
            // ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
            WriteEnum(method.CallingConvention & (MethodCallingConvention)0x1f, _callingConvention);

            //return type
            method.ReturnType.WriteTo(_o);
            _o.Write(' ');
            if (method.MethodReturnType.HasMarshalInfo)
            {
                WriteMarshalInfo(method.MethodReturnType.MarshalInfo);
            }

            _o.Write(method.IsCompilerControlled
                        ? DisassemblerHelpers.Escape(method.Name + "$PST" + method.MetadataToken.ToInt32().ToString("X8"))
                        : DisassemblerHelpers.Escape(method.Name));

            WriteTypeParameters(_o, method);

            //( params )
            _o.Write(" (");
            if (method.HasParameters)
            {
                _o.WriteLine();
                _o.Indent();
                WriteParameters(method.Parameters);
                _o.Unindent();
            }
            _o.Write(") ");
            //cil managed
            WriteEnum(method.ImplAttributes & MethodImplAttributes.CodeTypeMask, _methodCodeType);
            _o.Write((method.ImplAttributes & MethodImplAttributes.ManagedMask) == MethodImplAttributes.Managed ? "managed " : "unmanaged ");
            WriteFlags(method.ImplAttributes & ~(MethodImplAttributes.CodeTypeMask | MethodImplAttributes.ManagedMask), _methodImpl);

            _o.Unindent();
            OpenBlock();
            WriteAttributes(method.CustomAttributes);
            if (method.HasOverrides)
            {
                foreach (var methodOverride in method.Overrides)
                {
                    _o.Write(".override method ");
                    methodOverride.WriteTo(_o);
                    _o.WriteLine();
                }
            }
            WriteParameterAttributes(0, method.MethodReturnType, method.MethodReturnType);
            foreach (var p in method.Parameters)
            {
                WriteParameterAttributes(p.Index + 1, p, p);
            }
            WriteSecurityDeclarations(method);

            if (method.HasBody)
            {
                new MethodBodyDisassembler(_o)
                .Disassemble(method.Body);
            }

            CloseBlock("end of method " + DisassemblerHelpers.Escape(method.DeclaringType.Name) + "::" + DisassemblerHelpers.Escape(method.Name));
        }