Type.FullName with a Plus?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matthew Wieder

    Type.FullName with a Plus?

    CSharp, VS 2003.
    I'm writing a utility which takes an assembly and generates a proxy for
    that assembly in CSharp (much like the on in the Feb 2003 issue of MSDN
    for VB). It's almost finished, except for one issue.
    The proxy has three lines for each method - one declaring the return
    variable, another making the call and the third return the return
    variable. The line that declares the return variable is the product of
    the following code:
    localSnippet = new
    CodeSnippetStat ement(localMeth odInfo.ReturnTy pe.FullName + " localReturn;");
    localSnippet is then added to the Statements of localNewMethod (a
    CodeMemberMetho d) which had the following line of code:
    localNewMethod. ReturnType = new
    CodeTypeReferen ce(localMethodI nfo.ReturnType. FullName);

    The problem is that after the call to GenerateCodeFro mCompileUnit, the
    new class contains methods that look like this:
    public UberClass.MyRes ultType MethodName(Para mType param) {
    UberClass+MyRes ultType localReturn;
    localReturn = m_obj.MethodNam e(param);
    return localReturn;
    }
    Notice the plus "+" in the declaration of the localReturn variable.
    When stepping through, I noticed that in fact, the
    localMethodInfo .ReturnType.Ful lName string does, in fact, contain a plus
    between the UberClass and MyResultType, however, in the class
    declaration, it somehow gets evaluated correctly to remove the "+" and
    replace it with a "." How can I cause the declaration of the return
    variable to also get transformed from a "+" to a "."?

    thanks!

  • Mattias Sjögren

    #2
    Re: Type.FullName with a Plus?

    Matthew,
    [color=blue]
    >Notice the plus "+" in the declaration of the localReturn variable.[/color]

    + is the Reflection way to indicate a nested type.

    [color=blue]
    >How can I cause the declaration of the return
    >variable to also get transformed from a "+" to a "."?[/color]

    Replacing the + with a . should work if you only target C#. Something
    like

    string className = localMethodInfo .ReturnType.Ful lName;
    if ( className.Index Of( '+' ) != -1 )
    className = className.Repla ce( '+', '.' );
    localSnippet = new CodeSnippetStat ement(className + " localReturn;");



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Matthew Wieder

      #3
      Re: Type.FullName with a Plus?

      Isn't that a hack? There's no proper way to get the "+" to evaluate to
      a "." as it does in the method declaration?
      thanks!

      Mattias Sjögren wrote:[color=blue]
      > Matthew,
      >
      >[color=green]
      >>Notice the plus "+" in the declaration of the localReturn variable.[/color]
      >
      >
      > + is the Reflection way to indicate a nested type.
      >
      >
      >[color=green]
      >>How can I cause the declaration of the return
      >>variable to also get transformed from a "+" to a "."?[/color]
      >
      >
      > Replacing the + with a . should work if you only target C#. Something
      > like
      >
      > string className = localMethodInfo .ReturnType.Ful lName;
      > if ( className.Index Of( '+' ) != -1 )
      > className = className.Repla ce( '+', '.' );
      > localSnippet = new CodeSnippetStat ement(className + " localReturn;");
      >
      >
      >
      > Mattias
      >[/color]

      Comment

      • Mattias Sjögren

        #4
        Re: Type.FullName with a Plus?

        Matthew,
        [color=blue]
        >Isn't that a hack? There's no proper way to get the "+" to evaluate to
        >a "." as it does in the method declaration?[/color]

        Replacing + with . is exactly what the C# CodeDom generator does. You
        can see that for yourself if you look at the code for

        Microsoft.CShar p.CSharpCodeGen erator.GetBaseT ypeOutput()

        in the System.dll assembly with a decompiler.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org

        Please reply only to the newsgroup.

        Comment

        Working...