Ask a questionAsk a question
 

AnswerGeneric parameter in method

  • Friday, November 06, 2009 2:17 PMSyslock Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    I have a class:

    Class ClassA
    {
       public void doSomething()
       {
          // This line doesn't work and needs fix.
          IPresenterBase<T> presenter = container.Resolve<IPresenterBase<T>>();
       }
    }

    The method signature for Resolve:

    T Resolve<T>();

    How should i change the implementation of class "ClassA" or method "doSomething" so that the line gets fixed meaning i could pass a generic type?

Answers

All Replies

  • Friday, November 06, 2009 2:25 PMVitaliy Liptchinsky Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    public void doSomething<T>()
       {
          // This line doesn't work and needs fix.
          IPresenterBase<T> presenter = container.Resolve<IPresenterBase<T>>();
       }

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
  • Friday, November 06, 2009 2:25 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You need to give a context for "T".  What type is T here?

    Either create a generic type parameter for the method, or create one for the class.

    Class ClassA<T>
    {
       public void doSomething()
       {
          // This line doesn't work and needs fix.
          IPresenterBase<T> presenter = container.Resolve<IPresenterBase<T>>();
       }
    }

    or

    Class ClassA
    {
       public void doSomething<T>()
       {
          // This line doesn't work and needs fix.
          IPresenterBase<T> presenter = container.Resolve<IPresenterBase<T>>();
       }
    }

    or decalare the type parameter directly:

    Class ClassA
    {
       public void doSomething()
       {
          // This line doesn't work and needs fix.
          IPresenterBase<ISomeInterface> presenter = container.Resolve<IPresenterBase<ISomeInterface>();
       }
    }
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 2:34 PMSyslock Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Weird for i get an error when i tried first and second proposal:

    The type 'T' cannot be used as type parameter 'T' in the generic type or method 'PLVS.Infrastructure.Presenters.IPresenterBase<T>'. There is no boxing conversion or type parameter conversion from 'T' to 'PLVS.Infrastructure.Views.IViewBase'.

    What i didn't mention in my first posting is the definition of IPresenterBase:

        public interface IPresenterBase<T> where T : IViewBase
        {
            T View { get; set; }
        }
  • Friday, November 06, 2009 2:35 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    The constraints have to be honored everywhere... hence:

    Class ClassA<T> where T : IViewBase
    {
       public void doSomething()
       {
          // This line doesn't work and needs fix.
          IPresenterBase<T> presenter = container.Resolve<IPresenterBase<T>>();
       }
    }

    or

    Class ClassA
    {
       public void doSomething<T>() : where T : IViewBase
       {
          // This line doesn't work and needs fix.
          IPresenterBase<T> presenter = container.Resolve<IPresenterBase<T>>();
       }
    }

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 3:07 PMSyslock Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks.

    How can i change the interface property to a generic type? e.g. 

        public interface IPresenterBase<T> where T : IViewBase
        {
            T View { get; set; }
        }

    I don't want the interface to have a generic parameter only the property.

    Something like (needs fixing):

        public interface IPresenterBase
        {
            T View  where T : IViewBase { get; set; }
        }
  • Friday, November 06, 2009 6:15 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Sorry, you can't do that.  You can't declare a generic property without a generic class or interface surrounding it.  You can declare a generic method without the generic interface surrounding it, which is what I showed above, but you can't ever have a simply generic property without some sort of overarching context, such as a generic parameter in the class or interface declaration. 
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser