473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting correct method signature


How to get syntactically correct signature which compiles for code template
grneration ?
I tried code below but it creates syntactically incorrect signature.

Andrus.

using System;
using System.Collections.Generic;

public class MainClass
{
static string GetSignature(Type xTheType, string method)
{
var xModule = xTheType.Module;
var xTheMethod = xTheType.GetMethod(method);
return xTheMethod.ToString();
}

public static void Main()
{
// Observed: Void Test(System.Object,
System.Collections.Generic.List`1[System.String])
// Expected: public void Test(object p1, List<stringp2)
// or some other syntactically correct signature
Console.WriteLine(GetSignature(typeof(MainClass), "Test"));
}

public void Test(object p1, List<stringp2)
{
}
}

Oct 31 '08 #1
7 4675
"Andrus" <ko********@hot.eewrote in message
news:ug**************@TK2MSFTNGP05.phx.gbl...
// Observed: Void Test(System.Object,
System.Collections.Generic.List`1[System.String])
The `x in the type name tells you how many parameters the generic takes, and
the parameters (if that's the right word) are then listed afterwords in
brackets, comma delimited, I believe. (Check it on a Dictionary.) So you'd
just need to do a little parsing and substringing to rewrite the type in a
C#-friendly manner.
Oct 31 '08 #2
Jeff,
> // Observed: Void Test(System.Object,
System.Collections.Generic.List`1[System.String])

The `x in the type name tells you how many parameters the generic takes,
and the parameters (if that's the right word) are then listed afterwords
in brackets, comma delimited, I believe. (Check it on a Dictionary.) So
you'd just need to do a little parsing and substringing to rewrite the
type in a C#-friendly manner.
Thank you.
Where to find some sample code for this?
How to get method parameter names also as defined in method code?
Why it outputs Void with upper case ?

andrus.

Oct 31 '08 #3
"Andrus" <ko********@hot.eewrote in message
news:eN**************@TK2MSFTNGP02.phx.gbl...
>> // Observed: Void Test(System.Object,
System.Collections.Generic.List`1[System.String])

The `x in the type name tells you how many parameters the generic takes,
and the parameters (if that's the right word) are then listed afterwords
in brackets, comma delimited, I believe. (Check it on a Dictionary.) So
you'd just need to do a little parsing and substringing to rewrite the
type in a C#-friendly manner.
Where to find some sample code for this?
Sample code for what? Manipulating strings? I'm sure you can do something
that simple....
How to get method parameter names also as defined in method code?
Hmm, I don't deal with this kind of Reflection very often. Let me take a
look.

Okay, from a cursory glance at MSDN, calling MethodInfo.GetParameters() gets
you an array of ParameterInfo objects. These objects have a Name property.
Does that work for you?
Why it outputs Void with upper case ?
I'm willing to bet that that's the Frameworks "neutral" version of the
keyword, just like you're getting a type of System.Object instead of the
C#-specific "object" keyword.

I just noticed you're using the "var" keyword. I don't use the 3.x Framework
(I'm basing my responses on 2.0) so I don't know if anything's changed. I
doubt it, since my MSDN is current.
Oct 31 '08 #4
Jeff,
Sample code for what? Manipulating strings? I'm sure you can do something
that simple....
Sample code for C# method signature disassembler.

Andrus.

Oct 31 '08 #5
"Andrus" <ko********@hot.eewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Sample code for what? Manipulating strings? I'm sure you can do something
that simple....

Sample code for C# method signature disassembler.
Well, you're kind of in luck today. I dabbled in writing my own object
browser a while back, and I still have the code. It's in VB, and this was
pre-generics, so you're not going to get exactly what you want (note the
TODO near the end) but it should be a start.

Private Function GetSignature(ByVal method As MethodInfo) As String
Dim firstParam As Boolean = True
' TODO: Don't hardcode the function open/close characters
Dim sigBuilder As StringBuilder = New StringBuilder(method.Name & "(")
Dim displayType As String

For Each param As ParameterInfo In method.GetParameters()
If param.ParameterType.FullName IsNot Nothing Then
Dim paramType As String = param.ParameterType.FullName

If paramType.EndsWith("&") Then
paramType = paramType.Substring(0, paramType.Length - 1)
End If

Select Case paramType.ToLower()
Case "system.object"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "object"
Else
displayType = "Object"
End If
Case "system.string"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "string"
Else
displayType = "String"
End If
Case "system.int32"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "int"
Else
displayType = "Integer"
End If
Case "system.int16"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "short"
Else
displayType = "Short"
End If
Case "system.byte"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "byte"
Else
displayType = "Byte"
End If
Case "system.int64"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "long"
Else
displayType = "Long"
End If
Case "system.boolean"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "bool"
Else
displayType = "Boolean"
End If
Case "system.char"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "char"
Else
displayType = "Char"
End If
Case "system.datetime"
If m_viewStyle = ViewStyle.CSharp Then
displayType = paramType ' No equivalent in C#
Else
displayType = "Date"
End If
Case "system.double"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "double"
Else
displayType = "Double"
End If
Case "system.single"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "float"
Else
displayType = "Single"
End If
Case "system.sbyte"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "sbyte"
Else
displayType = "SByte"
End If
Case "system.uint32"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "uint"
Else
displayType = "UInteger"
End If
Case "system.uint16"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "ushort"
Else
displayType = "UShort"
End If
Case "system.uint64"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "ulong"
Else
displayType = "ULong"
End If
Case "system.decimal"
If m_viewStyle = ViewStyle.CSharp Then
displayType = "decimal"
Else
displayType = paramType ' No equivalent in VB
End If
Case Else
displayType = paramType
End Select

If firstParam Then
firstParam = False
Else
sigBuilder.Append(", ")
End If

If m_viewStyle = ViewStyle.CSharp Then
If param.ParameterType.IsByRef Then
sigBuilder.Append("ref ")
ElseIf param.IsOut Then
sigBuilder.Append("out ")
End If
Else
If param.ParameterType.IsByRef Then
sigBuilder.Append("ByRef ")
End If
End If

sigBuilder.Append(displayType)
Else
' TODO: Handle generics
Return String.Empty
End If
Next

sigBuilder.Append(")")

Return sigBuilder.ToString()
End Function
Oct 31 '08 #6
Well, you're kind of in luck today. I dabbled in writing my own object
browser a while back, and I still have the code. It's in VB, and this was
pre-generics, so you're not going to get exactly what you want (note the
TODO near the end) but it should be a start.
Thank you. Using this I created code below.
If I pass method name containing generic type parameters to GetMSignature
GetMethod returns null.

How to change this that it works for generic method also: builds signature
with generic type parameters and their constraints ?

Andrus.
static string GetMSignature(Type xTheType, string method)
{
var xModule = xTheType.Module;
MethodInfo xTheMethod = xTheType.GetMethod(method);
xTheMethod = xTheType.GetMethod(method);
return GetSignature(xTheMethod);
}
static string GetSignature(MethodInfo method)
{
bool firstParam = true;
StringBuilder sigBuilder = new
StringBuilder(ReflectionUtil.TypeName(method.Retur nType));
sigBuilder.Append(' ');
sigBuilder.Append(method.Name + "(");
foreach (ParameterInfo param in method.GetParameters())
{
if (firstParam)
firstParam = false;
else
sigBuilder.Append(", ");

if (param.ParameterType.IsByRef)
sigBuilder.Append("ref ");
else if (param.IsOut)
sigBuilder.Append("out ");
sigBuilder.Append(TypeName(param.ParameterType));
sigBuilder.Append(' ');
sigBuilder.Append(param.Name);
}
sigBuilder.Append(")");
return sigBuilder.ToString();
}

/// <summary>
/// Get full type name with full namespace names
/// </summary>
/// <param name="type">Type. May be generic or nullable</param>
/// <returns>Full type name, fully qualified namespaces</returns>
public static string TypeName(Type type)
{
Type nullableType = Nullable.GetUnderlyingType(type);
if (nullableType != null)
return nullableType.Name + "?";

if (!type.IsGenericType)
switch (type.Name)
{
case "String": return "string";
case "Int32": return "int";
case "Decimal": return "decimal";
case "Object": return "object";
case "Void": return "void";
default: return type.FullName;
}

StringBuilder sb = new StringBuilder(type.Name.Substring(0,
type.Name.IndexOf('`'))
);
sb.Append('<');
bool first = true;
foreach (Type t in type.GetGenericArguments())
{
if (!first)
{
sb.Append(',');
first = false;
}
sb.Append(TypeName(t));
}
sb.Append('>');
return sb.ToString();
}

Oct 31 '08 #7
"Andrus" <ko********@hot.eewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Well, you're kind of in luck today. I dabbled in writing my own object
browser a while back, and I still have the code. It's in VB, and this was
pre-generics, so you're not going to get exactly what you want (note the
TODO near the end) but it should be a start.

Thank you. Using this I created code below.
If I pass method name containing generic type parameters to GetMSignature
GetMethod returns null.

How to change this that it works for generic method also: builds signature
with generic type parameters and their constraints ?
I'm afraid that's up to you. The MethodInfo class has methods and properties
like GetGenericArguments() and IsGeneric method. As you can see, I never
implemented them, nor did I even look into them.
Oct 31 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sibyl | last post by:
Is there any way to get the name of a class without an instance (i.e., object of the class)? I am working with log4j, and would like a uniform way to name loggers without typing in the name of the...
1
by: Michael P. Soulier | last post by:
Hello, For a GUI app I've tried resetting sys.excepthook to my own exceptionhandler bound method, which accepts a type, value and traceback object. Now, the traceback module has print_exc...
13
by: Kenneth Baltrinic | last post by:
This isn't a problem from the standpoint that its easy to work around. However, I am very currious as to the correctness of this behavior. I am developing an MDI application. In the application...
0
by: Cordell Lawrence | last post by:
Okay guys, We are wondering if this is a bug in Framework 2.0.40607 and looking for some clarification on the issue. Take a look at the folowing code. public delegate bool BoundryTest(int...
3
by: Grant Schenck | last post by:
I have a COM object which has a method which returns a variant which contains a safe array of variants. Does anyone know what the syntax would be to access the actual elements as integers fro a...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
15
by: Brady Love | last post by:
I am currently working an an app that will post and edit blogs on blogger. Right now I have it so I can recive a list of the blogs and the content of those blogs using Atomizer. This is my first...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
4
by: BrianKE | last post by:
I am attempting to create an app that will run "on top" of another app. My app will create a transparent window over the other app for the purpose of displaying information. I am able to create...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.