So, this might seem similar to other issues with the same title out there - but I couldn't find a solution to the specific issue i'm having. Please feel free to link a solution if there's one already out there.
Background:
I have this method which needs to accept a generic interface as a parameter type (as other objects also need to call this function)
I have simplified the problem to not include the exact details of the problem, as it's confidential. I have replaced it with Animals. typeOfAnimal below is a Fox, Cat, etc...
The IDefinition<T>
object is an interface encompassing any definition of any Animal to be passed in.
The 'problem' i'm facing isn't exactly a blocking issue, this works, but it's using a standard I wouldn't be all too comfortable using..
It involves creating an instance of DefinitionOfFox
, using this as an instance of the class to call a method on the class, and then passing this instance into that method as a param because it's needed further in that method. As expected, this works fine (passing in DefinitionOfFox
as a type IDefinition<T>
), but I want to refactor it so I'm not passing in the DefinitionOfFox
object to the DefinitionOfFox
class... and instead the DefinitionOfFox
class creates the instance itself.
CURRENT STATE
public class Fox : Animal {
public void Init()
{
DefinitionOfFox definition = new DefinitionOfFox();
definition.Method1<Fox>(this, definition);
}
}
public class DefinitionOfFox : IDefinition<Fox>
{
public void Method1<T>(T typeOfAnimal, IDefinition<T> definition)
{
OtherService otherService = new OtherService();
otherService.Method2<T>(typeOfAnimal, definition);
}
}
All works fine in the current state, but it doesn't sit right with me passing the class instance in as a param to it's own class.
IDEAL STATE
public void Init()
{
DefinitionOfFox definition = new DefinitionOfFox();
definition.Method1<Fox>(this);
}
public class DefinitionOfFox : IDefinition<Fox>
{
public void Method1<T>(T typeOfAnimal)
{
DefinitionOfFox definition = new DefinitionOfFox();
OtherService otherService = new OtherService();
otherService.Method2<T>(typeOfAnimal, definition);
}
}
OtherService
class and Method2
NB This method must be called from the DefinitionOfFox class, not from the Fox class.
public class OtherService
{
public void Method2<T>(T typeOfFox, IDefinition<T> definition)
{
}
}
I get a compiler error for definition
when trying to pass this into the OtherService.Method2()
method. It complains that it can't convert DefinitionOFox
to IDefinition<T>
.
How come this works in the current state, but not in the Ideal state? And is there a way I can have my Ideal state? It is the same object that is getting passed into the method, it's just created at a different time.
error says Argument 2: Cannot convert from ....DefinitionOfFox to .... IDefinition
If this doesn't make sense, I can elaborate - no problem. Just let me know
Thanks for your time!
CodePudding user response:
It seems that Method1
just should not be generic or have non-generic overload:
public class DefinitionOfFox : IDefinition<Fox>
{
public void Method1(Fox typeOfAnimal)
{
DefinitionOfFox definition = new DefinitionOfFox();
OtherService.Method2(typeOfAnimal, definition); // or OtherService.Method2(typeOfAnimal, this); based on goals
}
}
How come this works in the current state, but not in the Ideal state
Because in the ideal state the generic parameter T
is closed with Fox
so it "matches" for T
and IDefinition<T>
, for the "ideal" state imagine if your code was compiling then what should happen in the case of Method1<Rabbit>(...)
invocation is DefinitionOfFox
suitable for IDefinition<Rabbit>
parameter?
If you still want your method to be generic and you want to create a new instance of IDefinition<T>
inside then you will need to generic parameters with corresponding constraints:
public class DefinitionOfFox : IDefinition<Fox>
{
public void Method1<T, TDefinition>(T typeOfAnimal) where TDefinition : IDefinition<T>, new()
{
IDefinition<T> definition = new TDefinition();
OtherService otherService = new OtherService();
otherService.Method2<T>(typeOfAnimal, definition);
}
}
CodePudding user response:
Yep, that sorted it. Having removed the Generic use in Method1, This now works as the Current state in my description. Thank you!