Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Calling a method on a generic type?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Philip_L  
View profile  
 More options Apr 12 2009, 1:10 am
From: Philip_L <Philip.Laure...@gmail.com>
Date: Sat, 11 Apr 2009 22:10:56 -0700 (PDT)
Local: Sun, Apr 12 2009 1:10 am
Subject: Calling a method on a generic type?
If I have a Singleton class that looks like this:

public interface ICreate<T>
    {
        T Create();
    }

    public sealed class Singleton<TFactory, T>
        where TFactory : ICreate<T>, new()
    {
        Singleton()
        {
        }

        public static T Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            static Nested()
            {
            }

            internal static readonly T instance = new TFactory().Create
();
        }
    }

How do I actually get a MemberReference to the static Instance
property so I can call the get_Instance method?

This is the code that I have so far:

            var genericSingletonType = module.Import(typeof
(Singleton<,>));
            var singletonType = new GenericInstanceType
(genericSingletonType);
            singletonType.GenericArguments.Add(creatorType);
            singletonType.GenericArguments.Add(itemType);

The problem is that since the singletonType is actually a
GenericInstanceType, I can't seem to access the get_Instance method
(or frankly, I'm not sure where to access it). What makes this even
more confusing is that when I look at the operand of a compiler-
emitted Call instruction to the Singleton<TFactory, T>.Instance
property, it lists the get_Instance method as a MemberReference. So my
question is this: How do I emit a call to the instantiated
Singleton<TFactory, T>.get_Instance method? Thanks in advance for your
help!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Philip_L  
View profile  
 More options Apr 12 2009, 3:51 am
From: Philip_L <Philip.Laure...@gmail.com>
Date: Sun, 12 Apr 2009 00:51:12 -0700 (PDT)
Local: Sun, Apr 12 2009 3:51 am
Subject: Re: Calling a method on a generic type?
Here's the updated code that I'm using to emit the Singleton<,> call:

// Call the Singelton<TFactory, TService>.Instance
            var genericSingletonType = module.Import(typeof
(Singleton<,>));
            var serviceTypeParameter =
genericSingletonType.GenericParameters[1];

            var singletonType = new GenericInstanceType
(genericSingletonType);
            singletonType.GenericArguments.Add(creatorType);
            singletonType.GenericArguments.Add(serviceType);

            var getInstanceMethod = new MethodReference
("get_Instance", singletonType, serviceTypeParameter, false, false,
MethodCallingConvention.Default);
            module.MemberReferences.Add(getInstanceMethod);

            IL.Emit(OpCodes.Call, getInstanceMethod);

The problem is that PEVerify keeps reporting "Unable to resolve
token". Is there something that I'm doing wrong here?

On Apr 12, 1:10 pm, Philip_L <Philip.Laure...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Lotfi Gheribi  
View profile  
 More options Apr 12 2009, 4:22 pm
From: Lotfi Gheribi <gheri...@gmail.com>
Date: Sun, 12 Apr 2009 21:22:14 +0100
Local: Sun, Apr 12 2009 4:22 pm
Subject: Re: [mono-cecil] Re: Calling a method on a generic type?

> // Call the Singelton<TFactory, TService>.Instance
>            var genericSingletonType = module.Import(typeof
> (Singleton<,>));

I don't know why, but it seams that the Import method doesn't import
generic parameter constraints :s

var genericSingletonType = module.Import(typeof(Singleton<,>));


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Philip Laureano  
View profile  
 More options Apr 12 2009, 9:31 pm
From: Philip Laureano <philip.laure...@gmail.com>
Date: Mon, 13 Apr 2009 09:31:46 +0800
Local: Sun, Apr 12 2009 9:31 pm
Subject: Re: [mono-cecil] Re: Calling a method on a generic type?
I never managed to solve the bug--instead, I ended up emitting a whole
non-generic singleton class implementation. It wasn't pretty, but it
definitely worked :)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »