Get system generic type defenition from Post-Sharp defenition.

Do you program directly PostSharp.CodeModel or another low-level API and need help? Put your questions here and we will try to answer.

Get system generic type defenition from Post-Sharp defenition.

Postby Anonymous on Thu Jan 11, 2007 10:39 am

I have this method

public void PassArray(T[] workersArray)

Then I watch on ParameterDeclaration.ParameterType of workersArray it shows "{!!0[]}".
Then I try to get ParameterDeclaration.ParameterType.GetSystemType(null, null) of workersArray then it returns null. But I want to get System.Type of workersArray ({Name = "T[]" FullName = null}). How I can do it?
Anonymous
 

Re: Get system generic type defenition from Post-Sharp defenition.

Postby Denis on Thu Jan 11, 2007 10:44 am

Sorry, the Anonymous was I am.
Denis Bredikhin
Denis
 
Posts: 20
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Denis Bredikhin

Re: Get system generic type defenition from Post-Sharp defenition.

Postby gfraiteur on Thu Jan 11, 2007 11:05 am

I suppose it is because you do not pass generic arguments to GetSystemType (you pass null values). In System.Reflection, all generic arguments should be bound. You should pass at least one generic method argument from the proper context.Gael
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Get system generic type defenition from Post-Sharp defenition.

Postby Denis on Thu Jan 11, 2007 1:10 pm

But from reflection I can get Type for generic argument defenition ({Name = "T[]" FullName = null}) using type.GetMethod("PassArray").GetParameters()[0].ParameterType
Denis Bredikhin
Denis
 
Posts: 20
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Denis Bredikhin

Re: Get system generic type defenition from Post-Sharp defenition.

Postby gfraiteur on Fri Jan 12, 2007 9:17 am

This is because [i]System.Reflection[/i] implicitly binds the generic parameters of [i]ParameterType [/i]to the generic parameters of the method. In [i]System.Reflection[/i], everything is bound to a generic context. But in MSIL (and PostSharp), generic parameters are not bound to a context, you have to give the context yourself. In MSIL, the same type can be used in different contexts, so it is not possible to add a method "get system type in the parent context" at type level. This is a mater of semantics, a logical feature of MSIL, not a technical limitation of PostSharp.
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Get system generic type defenition from Post-Sharp defenition.

Postby Anonymous on Fri Jan 12, 2007 1:11 pm

Please, help me. How I can bind generic parameters of ParameterType to the generic parameters of the method like System.Reflection do? Should I use MapGenericArguments?
I have MethodDefDeclaration and I want to get System.Type for each parameter.
Anonymous
 

Re: Get system generic type defenition from Post-Sharp defenition.

Postby Denis on Fri Jan 12, 2007 1:19 pm

Sorry again.
Denis Bredikhin
Denis
 
Posts: 20
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Denis Bredikhin

Re: Get system generic type defenition from Post-Sharp defenition.

Postby gfraiteur on Sat Jan 13, 2007 6:22 pm

I changed all forums to member-only posting :).As for your question, it really depends on you what to do. In which generic context would you like to use the system type?

I suppose your context is the body of the method declaring the parameter. In this case your generic context, in terms of system types, is MethodInfo.GetGenericArguments(). You will get an array of types representing unbound generic arguments.
Gael Fraiteur, project leader
got good support? consider donating to the project.
gfraiteur
Site Admin
 
Posts: 608
Joined: Tue Dec 18, 2007 3:09 pm
Full Name: Gael Fraiteur
Company: postsharp.org

Re: Get system generic type defenition from Post-Sharp defenition.

Postby Denis on Sat Jan 19, 2008 4:20 am

Thanks, it seems I'm investigated it. :)


Last bumped by Anonymous on Sat Jan 19, 2008 4:20 am.
Denis Bredikhin
Denis
 
Posts: 20
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Denis Bredikhin


Return to PostSharp Core