Method argument of generic type

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

Method argument of generic type

Postby Ed_ward on Sun Dec 30, 2007 10:50 pm


Hi Gael
I have the next code
public abstract class Presenter<TView> where TView : class
{
    [ValidateArguments]
    public void AttachView( [NotNull]TView view )
    {}
}

[ValidateArguments] inherits MulticastAttribute, [NotNull] is just custom attribute used by advice to determine what code to emit.

When I handle the "view" method argument in my advice the ParameterType property of the instance of the ParameterInfo class returned by the parameter.GetReflectionWrapper(null, null) method is null.
How to distinguish arguments of a generic type from arguments of a concrete type in the right way?
How to get a type which I can use to emit code? C# compiler use "box" IL command with such types (box[/url] !TView[/url]) even when I specify "class" constraint on generic argument it is the reason why I need the type further to argument name.

best regards and Happy New Year
Ed.ward
Edward Pavlov
Ed_ward
 
Posts: 11
Joined: Sat Jan 12, 2008 4:55 pm
Full Name: Edward Pavlov

Re: Method argument of generic type

Postby gfraiteur on Tue Jan 01, 2008 5:11 pm

Edward,

You should pass the array of generic arguments to GetReflectionWrapper, so you have to map all generic type and method arguments of the current method to reflection types, and pass them as a parameter.

As for the second question, look at the interface ITypeSignature. There is a property IsGenericDefinition and IsGenericInstance.

To construct a "box", you shoud use BoxedTypeSignature.

Yup, generics are difficult...

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


Return to PostSharp Core