On Nov 5, 1:57 pm, "Jb Evain" <[EMAIL PROTECTED]> wrote:
> Hey,
>
> On 11/5/08, JasonBock <[EMAIL PROTECTED]> wrote:
>
> > I have a TypeReference. I want to turn it into a .NET Type (i.e
> > System.Type in mscorlib). Is there a way to do this in the API, either
> > directly or indirectly?
>
> There's no such API I'm aware of. To be honest it's the first time
> I've been asked for that.
>
> A simple way to do that would be by resolving types by names. Note
> that Cecil uses the CIL naming conventions, while System.Reflection
> has a custom naming scheme for nested types and generic definitions.
Here's the scenario that I'm trying to solve - maybe there's an
alternative way to do this that doesn't require a TypeReference ->
Type conversion.
I'm trying to output the values from the public instance properties on
a given type. I have the type of the property from the PropertyType
property, and it's a TypeReference. Now, I want to add this value to a
StringBuilder via its Append() method. Append() has numerous overloads
and I'd like to call the version that has the "best match" - i.e. if
the property type is int, I want to call Append(int), if it's a
string, I want to call Append(string).
So...I want to get the right append method by doing something like
this:
var append =
@this.Module.Import(typeof(StringBuilder).GetMethod("Append",
new Type[] { typeof(object) }));
The problem that you can see if that I want to use the TypeReference -
something like this:
var append =
@this.Module.Import(typeof(StringBuilder).GetMethod("Append",
new Type[] { ConvertToType(property.PropertyType) }));
where ConvertToType() is a mythical function that does the
TypeReference -> Type conversion.
The other alternative would be to get the method completely from the
Cecil API - i.e. get a TypeReference to StringBuilder, walk through
its methods in the Methods collection, and find the right match. Do
you think that would be a better option?
As a side note, it would be nice if the Cecil API has more GetXXX()
methods like the Reflection API does. Would make it a lot easier to
discover members.
Regards,
Jason
--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---