Type.FullName with a Plus?

  • Thread starter Thread starter Matthew Wieder
  • Start date Start date
M

Matthew Wieder

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
CodeSnippetStatement(localMethodInfo.ReturnType.FullName + " localReturn;");
localSnippet is then added to the Statements of localNewMethod (a
CodeMemberMethod) which had the following line of code:
localNewMethod.ReturnType = new
CodeTypeReference(localMethodInfo.ReturnType.FullName);

The problem is that after the call to GenerateCodeFromCompileUnit, the
new class contains methods that look like this:
public UberClass.MyResultType MethodName(ParamType param) {
UberClass+MyResultType localReturn;
localReturn = m_obj.MethodName(param);
return localReturn;
}
Notice the plus "+" in the declaration of the localReturn variable.
When stepping through, I noticed that in fact, the
localMethodInfo.ReturnType.FullName 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!
 
Matthew,
Notice the plus "+" in the declaration of the localReturn variable.

+ is the Reflection way to indicate a nested type.

How can I cause the declaration of the return
variable to also get transformed from a "+" to a "."?

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

string className = localMethodInfo.ReturnType.FullName;
if ( className.IndexOf( '+' ) != -1 )
className = className.Replace( '+', '.' );
localSnippet = new CodeSnippetStatement(className + " localReturn;");



Mattias
 
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!
 
Matthew,
Isn't that a hack? There's no proper way to get the "+" to evaluate to
a "." as it does in the method declaration?

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

Microsoft.CSharp.CSharpCodeGenerator.GetBaseTypeOutput()

in the System.dll assembly with a decompiler.



Mattias
 
Back
Top