C# Generics and Interface Inherits Interface - c#

Say I have two interfaces, IFeaturesA, IFeaturesB.
IFeaturesA has a set of signature methods. Let's say one is:-
public void printMe();
IFeaturesB implements IFeaturesA, and adds a new signature method, such as:-
public void printMeAlso();
Say I want to use a generic method, such as:-
public Check<E>(E passedItem)
{
}
If i passed in IFeaturesA, I want to be able to call the methods of this. If i pass in IFeaturesB, I want to be able to call the extra method, printMeAlso();
What is the best way to check for an interface type passed into a generic method and access its methods in C#? Is checking for the object type against the two interface types and then casting to the type the optimal way?

That does not fit the the generics.
Generics is a typeless reuse of the behaviour. If you need to check the type then it is not a good fit for generics. You may impose constraints but checking the actual type must not be done in the generic method..
All you are trying to abstract is the fact that you are passing an item. That is not an abstraction.

use two methods
public Check(IFeaturesA passedItem)
{
}
public Check(IFeaturesB passedItem)
{
}
add private methods to handle commonality

Don't think it's a good way to achieve what you want by defining an interface, simply define classe structure, like
public class FeaturesA
{
public virtual void printMe()
{
}
}
public class FeaturesB : FeaturesA
{
public override void printMe()
{
}
}
and after use in code, like
FeaturesA a = new FeaturesA();
FeaturesA b = FeaturesB();
public Check(A passedItem)
{
passedItem.printMe();
}
on Check(a) prints a, on Check(b) prints b
Hope this helps.

If you want to check the type of an item, use "is":
if (passedItem is IFeatureA) { }
If you want to constrain a generic to certain behaviors that can be defined by an interface or an abstract class, use "where":
public class foo<E> where E : IFeatureA
{
}
The latter case won't let you use IFeatureB, but any variable in the class of type E can use methods and properties defined in IFeatureA without checking type.
You might also want to look at type Dynamic, since it's closer to what you want to do.

I would check the type of E like
if(E.GetType().Equals(typeof(IFeaturesA)){ //call method...}

This looks like a call for another interface.
If you let both interfaces inherit from IPrintable which only contains a Print() method, and extend your Check<E>(E passedItem) with where E : IPrintable, you can call passedItem.Print().

try the following
Check(IFeaturesA f) {}
Check(IFeaturesB f){}
Check<T>(T obj) where T : IFeaturesA, IFeaturesB {}

Related

Is there a way to restrict access to some methods according to the type passed to a generic class?

I'm currently working on a generic class. I want to restrict accessibility to some methods when passing different types. Let's say it looks something like this:
public class MyGeneric<T>
{
public void Foo1(){}
public void Foo2(){}
}
I obviously can use the typeof operator to implement different behaviours but let's say that Foo1 counts the average of some numbers. If I pass in the type "Student" it can count the average of grades. But let's say I pass in "Car" and I don't want it to do anything. Having it just there and doing nothing with that type would be redundant. Is there a way to completely restrict access to it? Thanks!
You could use abstraction/inheritance for this.
Creating a class
public class MyGeneric<T>
{
public void Foo1(){}
}
which handles all 'global' functions, and then create a child class like this:
public class MyGenericChild<T> : MyGeneric<T> where T : IMyInterface
{
public void Foo2(){}
}
You can then use the specific child to handle the type specific methods.
If needed, you could expand this with a factory-pattern to retreive the correct generic class.

Common method for several generic classes to use

I have several classes that implement IMyInterface. The interface contains a method called Delete, so all classes define their own Delete method.
I also have a lot of spots in my code where I’m calling MyClass1.Delete(myClass1Id) or MyClass2.Delete(myClass2Id), etc.
I’m trying to define a generic, common method that takes a generic class parameter and a string for the Id.
I’m guessing it would look something like this, but I can’t find any relevant documentation on using generic classes in this fashion:
public static void DeleteGeneric<TMyClass>(string myId)
{
//call the relevant Delete method for the TMyClass class
}
There's no need to use generics here at all; just accept an instance of the interface:
public static void DeleteGeneric(IMyInterface deletable, string myId)
{
deletable.Delete(myId);
}
I think you want code like this:
public static void DeleteGeneric<TMyClass>(TMyClass myId) where TMyClass : IMyInterface
{
myId.Delete();
}
That says that TMyClass has to implement the IMyInterface interface and the parameter passed is of whatever type TMyClass is.
It might be simpler without generics
public static void DeleteGeneric(IMyInterface myId)
{
myId.Delete();
}
Disclaimer, I typed this code into the browser so there maybe syntax errors.

Explicit generic method implementation with 2 Type parameters -- only explicitly name 1 Type Parameter

I have an interface that has a generic method with two type parameters. I want to partially explicitly implement that generic method in a class. Is this possible? Some example code below:
public interface ISomeInterface
{
TResultType Results<TResultsType,TSearchCriteriaType>(TSearchCriteriaType searchCriteria);
}
public class SomeConcrete : ISomeInterface
{
public TResultsType Results<TResultsType, ConcreteSearchCriteria>(ConcreteSearchCriteria searchCriteria)
{
return (TResultsType)Results;
}
}
Do I have to explicitly implement both type parameters to make this work?
Do I have to explicitly implement both type parameters to make this work?
In order to implement this interface, your class must allow ANY types to be used for that method. (Any types which fit the constraints defined in the interface, which, in this case, since there are no constraints, means any type.)
You can't restrict the interface within a specific class implementing it, since this is a generic method (not a generic type), and there is no constraints which cause this to work properly.
In order to do what you wish, I think, you'd need to make the interface generic:
public interface ISomeInterface<TSearchCriteriaType>
{
TResultType Results<TResultsType>(TSearchCriteriaType searchCriteria);
}
You can then implement it as:
public class SomeConcrete : ISomeInterface<ConcreteSearchCriteria>
{
public TResultsType Results<TResultsType>(ConcreteSearchCriteria searchCriteria)
{
var results = GenerateResults();
return (TResultsType)results;
}
}
By making the interface generic on the search criteria, you allow your class to implement it based on a specific type for the search criteria.
I think this is what you're looking for:
public class SomeConcrete : ISomeInterface
{
TResultsType ISomeInterface.Results<TResultsType, TSearchCriteriaType>(TSearchCriteriaType searchCriteria)
{
return Results<TResultsType>((ConcreteSearchCriteria)(object)searchCriteria);
}
public TResultsType Results<TResultsType>(ConcreteSearchCriteria searchCriteria)
{
// return something
}
}
Note that this will fail at run time, though the compiler can see no problems:
ISomeInterface someInterface = new SomeConcrete();
var result = someInterface.Results<object, SomeOtherSearchCriteria>(null);
More generally, you're no longer implementing the interface as might be expected. You might want to make it ISomeInterface<TResultsType, TSearchCriteriaType>, then make SomeConcrete<TResultsType> : ISomeInterface<TResultsType, ConcreteSearchCriteria>. That way you are implementing the interface as expected, declaring it all strongly.
You can't specialize a generic method like that. If it's generic in the interface, it has to be generic in the implementing classes. If you move the type parameters to the interface itself, then you can do it (and you can also have variance annotations, making it covariant in the result type and contravariant in the search criteria type):
public interface ISomeInterface<out TResultsType, in TSearchCriteriaType> {
TResultsType Results(TSearchCriteriaType searchCriteria);
}
public class SomeConcrete<TResultsType> :
ISomeInterface<TResultsType, ConcreteSearchCriteria> {
public TResultsType Results(ConcreteSearchCriteria searchCriteria) {
...
}
}
Now your class is generic on the result type.
You can also have a class specialized in a certain result type, and generic in the criteria type:
public class IntSearcher<TSearchCriteriaType> :
ISomeInterface<int, TSearchCriteriaType> {
public int Results(TSearchCriteriaType searchCriteria) {
...
}
}
I think this is a more consistent design, since both generic parameters are at the same level (and it looks like they should go together). But only you can say whether this is better for your code or not.
Yes, I think you do, otherwise you are not directly implementing the interface.
Your Implementation is not as generic as the interface contract.

Can I define/constrain a member as implementing two interfaces, without generics?

The following code shows what I would like to do; that is, I would like to constrain anObject, so that it can be used as a parameter to various methods with use IInterfaceOne or IInterfaceTwo, where neither inherits from the other.
public interface IInterfaceOne { }
public interface IInterfaceTwo { }
public class Implementation : IInterfaceOne, IInterfaceTwo
{
}
public interface IInterfaceOneAndTwo : IInterfaceOne, IInterfaceTwo { }
public class UsingImplementation
{
IInterfaceOneAndTwo anObject = (IInterfaceOneAndTwo)(new Implementation()); //fails because Implementation doesnt acctually implement IInterfaceOneAndTwo
}
This example fails however as IInterfaceOneAndTwo is an interface in its own right, and Implementation does not implement it.
I know if I used generics I could constrain them, but I am wondering, if there is a way to do this without generics?
Is there a way to say anObject shall implement IInterfaceOne and IInterfaceTwo, without using IInterfaceOneAndTwo?
Not the way you have it currently. Only generic constraints have that ability.
You could rewrite it to use generics:
public class UsingImplementation<T>
where T : IInterface1, IInterface2, new()
{
T anObject = new T();
void SomeMethod() {
anObject.MethodFromInterface1();
}
}
You can also have generic methods, not only generic classes
public void DoSomething<T>(T value)
where T : IInterface1, IInterface2
{
value.DoInterface1Things();
value.DoInterface2Things();
}
Or
public void DoSomething<T>()
where T : IInterface1, IInterface2, new()
{
T anObject = new T();
}
You can't do that in C# without generics but there is an alternative workaround to solve the problem without generics that was not mentioned here and might fit for you. This style is often used together with the IoC principle. You could inject the same object twice. Let me change your sample quite a bit...
public interface IInterfaceOne { void Hello(); }
public interface IInterfaceTwo { void World(); }
public class Implementation : IInterfaceOne, IInterfaceTwo
{
public void Hello() { };
public void World() { };
}
public class UsingImplementation
{
private readonly IInterfaceOne one;
private readonly IInterfaceTwo two;
public UsingImplentation(IInterfaceOne one, IInterfaceTwo two)
{
this.one = one;
this.two = two;
}
// do the stuff you want to do with an IInterfaceOne using field one
public DoSomeThingWithOne() { one.Hello(); }
// do the stuff you want to do with an IInterfaceTwo using field two
public DoSomeThingWithTwo() { two.World(); }
}
Then you could wire up the things this way:
var oneAndTwo = new Implementation();
var a = new UsingImplementation(oneAndTwo, oneAndTwo);
// operates on the first param (which is the same as the second)
a.DoSomeThingWithOne();
// operates on the second param (which is the same as the first)
a.DoSomeThingWithTwo();
Have a look for IoC principle (Inversion of Control) and Dependency Injection and you'll find more solutions similiar to this one.
This way you don't need to create an extra Interface combining InterfaceOne and InterfaceTwo, two.
"Incoming" generic class parameters and generic method parameters can combine types, but there is no facility for variables or fields to represent "composite" types. Further, in order to pass an object to a parameter of a generic type which combines multiple constraints, the object must be cast to a type which in fact implements all of those constraints. This can be difficult.
For example, suppose class Foo and Bar both implement Intf1 and Intf2. One wishes to write a function AddToList<T>(thing as T) where T:Intf1,Intf2. Such a function will perfectly happily accept objects of type Foo or Bar. Suppose, however, one wishes to use such a function to add all objects to the same list (which might be a mix of Foo, Bar, and any number of other types that also happen to implement Intf1 and Intf2) and then later pass those objects to a function whose parameter is likewise constrained to implement both Intf1 and Intf2. One could cast to Foo any object which happened to be a Foo, and cast to Bar any object which happened to be a Bar, but if other types are written which also handle Intf1 and Intf2, it would be difficult to deal with them.
It is possible to solve the problem, somewhat awkwardly, without using Reflection or other such tricks. Define an interface IActUpon<Base1, Base2> with a method ActUpon<thingType>ActUpon(thingType thing) where thingType: Base1, Base2. Implementations of such a method will be able to pass parameter thing to other methods requiring generic method parameter constrained to Base1 and Base2. The biggest difficulties with such an approach are that one must write separate code for each possible number of constraints, and that in many places where one would have used a lambda expression one will instead have to write an implementation of IActUpon....
If this is desirable then there has to be a logical connection between IInterfaceOne and IInterfaceTwo and the implementing class should implement the combined interface:
class Implementation : IInterfaceOneAndTwo { ... }
If this is not possible, because it's not (all) your code then you may have to rethink the UsingImplementation. It simply doesn't fit the available surface.

Inferring C# Generic Type of Subclass

I have a generic class Proxy<T>, and I want to write another generic class with its type parameter being a Proxy.
I want to write:
public class MyClass<U> where U : Proxy<T>
but the compiler reports The type or namespace name T could not be found.
A solution I've found is to declare it like this:
public class MyClass<U, T> where U : Proxy<T>
but this seems clumsy as the client will have to declare two type parameters, like this:
public class SomeClass { ... }
public class SomeProxy : Proxy<SomeClass> { ... }
and then in a client somewhere:
var proxyWrapper = new MyClass<SomeProxy, SomeClass>();
How can I do this without having to have two generic types on MyClass. After all, if we know the first is SomeProxy, it should follow that the second is SomeClass.
Maybe something like this would do the job, too?
class Test<T> {
public Test(Proxy<T> proxy) { this.MyProxy = proxy; }
public Proxy<T> MyProxy { get; private set; }
}
Sorry, you just can't do this in C# without having MyClass generic on both types (unless you want to use reflection to create instances of it.)
You can have an interface IMyClass<SomeProxy> and a factory method that creates and returns an instance of MyClass<SomeProxy, SomeClass>. You may need to create the instance using Reflection.
I have a code example here of a similar situation: the end user only cares about a single type parameter, but the implementation needs to have two. In my example, I don't have to use Reflection to create the instance, but it sounds like you may need to.
What you're trying to do is possible using compile-time constructs such as C++ templates, but not run-time constructs such as C# generics.
If you want T to remain generic in Myclass, then the MyClass instance still needs to resolve all internally used generic types and you HAVE TO declare it somewhere. The way to go is the verbose way you mentioned:
public class MyClass<U, T> where U : Proxy<T>
If you don't care about the generic type T in MyClass then create interface and use it instead:
public interface IProxy { ... }
public class SomeClass { ... }
public class SomeProxy : Proxy<SomeClass>, IProxy { ... }
public class MyClass<U> where U : IProxy
and then in a client somewhere:
var proxyWrapper = new MyClass<SomeProxy>();
But do note that you cannot use type T in your interface declaration and Type U is now more general then before.
It turns out that all of the SomeProxy classes I want to deal with actually just override one method of Proxy<T> which has the signature:
T LoadInternal(Identifier id)
So, what I've done is created an internal class inside MyClass which takes a Func<Identifier, T> in its constructor. I can then pass a Func<Identifier, T> as a parameter to the constructor of MyClass and use my subclass in place of SomeProxy.
Seems a bit convoluted, but it works for me. To summarise, I now have:
public class MyClass<T>{
private SomeProxy theProxy;
public MyClass(Func<Identifier, T> loadDelegate){
theProxy = new SomeProxy(loadDelegate);
}
/* Other methods here */
class SomeProxy : Proxy<T>{
private Func<Identifier, T> m_loadInternal;
public SomeProxy(Func<Identifier, T> loadInternal){
m_loadInternal = loadInternal;
}
protected override T LoadInternal(Identifier id){
return m_loadInternal(id);
}
}
}
So, from client code, instead of writing a class which extends Proxy and then overriding LoadInternal in that class, I just create MyClass using:
var myClass = new MyClass<T>(x => CodeWhichReturnsT());
How can I do this without having to have two generic types on MyClass. After all, if we know the first is SomeProxy, it should follow that the second is SomeClass.
Although you seem to have found an answer to the main part of the question, I figured I'd offer my understanding about this part. It sounds like you wish you could do something like this:
class Proxy<T>
{
T Value { get; set; }
}
class MyClass<U> where U : Proxy<> { }
and have the compiler fill in the Proxy type parameter when you provide U. Since you have declared U as inheriting from Proxy, you must intend to use one of the methods on Proxy, that probably use the T parameter, like so:
class MyClass<U> where U : Proxy<>
{
void SomeMethod(U parameter)
{
var local = parameter.Value;
//more code here...
}
}
Now, what is the compiler supposed to infer for local here? This is the main problem I see that makes such a feature, if possible, hard to implement. If you don't want to use any methods that use the generic type of Proxy, you could instead make a non-generic base class and use that for U and sidestep the entire problem.
I am not a compiler writer, but a couple possibilities of how this could be dealt with come to mind. It could just say object (or whatever other restriction you put on the type parameter in Proxy), but that doesn't seem quite right or quite what normal generics seem to do. This would also require the CLR to allow open generic types as a constraint on the generic parameter, which I doubt it does. The other option I could see is for the type to actually have the second type parameter, and the compiler to give you syntactic sugar to make it easier.
Any way you go, this feature seems like a lot of work for a little benefit in what is probably a rare scenario, thus not likely to make the cut to get implemented.

Categories