.NET Framework Developer Center >
.NET Development Forums
>
.NET Base Class Library
>
Generic parameter in method
Generic parameter in method
- 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
- 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 Wiki • LinkedIn • ForumsBrowser- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 6:22 AM
- 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 Wiki • LinkedIn • ForumsBrowser- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 6:22 AM
All Replies
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/- 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 Wiki • LinkedIn • ForumsBrowser - 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; }
} - 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 Wiki • LinkedIn • ForumsBrowser- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 6:22 AM
- 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; }
} - 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 Wiki • LinkedIn • ForumsBrowser- Marked As Answer byeryangMSFT, ModeratorThursday, November 12, 2009 6:22 AM


