locked
generic method as extension method RRS feed

  • Question

  • I have a statement that looks like so (T is an application interface deriving from IServiceInterface)
      T theInterface = ServiceBroker.GetInstance<T>(type);  //type is of type Type :-)

    I am trying to write an extension method, T GetServiceBrokerInterface<T>, to wrap this call. Here's a usage scenario for the GetServiceBrokerInterface extension method.

    IMessageFactory mesgFactory = (IMessageFactory) (this.GetServiceBrokerInterface<IMessageFactory>);

    And I get this error:

    Error 1 Cannot convert method group 'GetServiceBrokerInterface' to non-delegate type 'Philips.PmsGxr.Cat.core.interfaces.IServiceInterface'. Did you intend to invoke the method? V:\PCF_CAT\Test\Cat.Net.Test\MessageFactoryTest.cs 14 45 Cat.Net.Test

    The method definition of GetServiceBrokerInterface is below.


    Where the syntax error?  Thanks...


     public static class ExtensionMethods  
        {  
     
            /// <summary> 
            /// Gets MessageFactory  
            /// </summary> 
            public static T GetServiceBrokerInterface<T>(this Object anyObject) where T : IServiceInterface  
            {  
                Type type = typeof(T);  
                 
                T theInterface = ServiceBroker.GetInstance<T>(type);  
     
                if (theInterface == null)  
                    return default(T);  
     
                if (type.IsAssignableFrom(theInterface.GetType()))  
                {  
                    return (T)theInterface;  
                }  
                else  
                    return default(T);  
     
            }  
        } 

    Paul
    Wednesday, February 25, 2009 2:41 PM

Answers

  • It's not just the parenthesis that I changed.

    I am actually calling the method, whereas you are not calling the method. Just to be more clear, use this :

    IMessageFactory mesgFactory = (IMessageFactory) (this.GetServiceBrokerInterface<IMessageFactory>)();

    I added back the parenthesis to avoid any confusion, see the () at the end. Your code does not have that.

    http://blog.voidnish.com
    • Marked as answer by Schwartzberg Wednesday, February 25, 2009 5:44 PM
    Wednesday, February 25, 2009 3:51 PM

All replies

  • Shouldn't

    IMessageFactory mesgFactory = (IMessageFactory) (this.GetServiceBrokerInterface<IMessageFactory>);

    be

    IMessageFactory mesgFactory = (IMessageFactory) this.GetServiceBrokerInterface<IMessageFactory>();

    http://blog.voidnish.com
    • Proposed as answer by JohnGrove Wednesday, February 25, 2009 3:45 PM
    Wednesday, February 25, 2009 3:03 PM
  • nope - the result is the same without the parenthesis.
    Thanks, Paul
    Wednesday, February 25, 2009 3:48 PM
  • It's not just the parenthesis that I changed.

    I am actually calling the method, whereas you are not calling the method. Just to be more clear, use this :

    IMessageFactory mesgFactory = (IMessageFactory) (this.GetServiceBrokerInterface<IMessageFactory>)();

    I added back the parenthesis to avoid any confusion, see the () at the end. Your code does not have that.

    http://blog.voidnish.com
    • Marked as answer by Schwartzberg Wednesday, February 25, 2009 5:44 PM
    Wednesday, February 25, 2009 3:51 PM
  •  ...  i guess, its time for me to go home :-)
    Thanks, Paul
    Wednesday, February 25, 2009 5:46 PM