.NET General Method

I am trying to understand the concept of .NET Generics and actually use them in my own code, but I have a problem all the time.

Can someone try to explain to me why the following setting does not compile?

public class ClassA
{
    ClassB b = new ClassB();

    public void MethodA<T>(IRepo<T> repo) where T : ITypeEntity
    {
        b.MethodB(repo);
    }
}

public class ClassB
{
    IRepo<ITypeEntity> repo;

    public void MethodB(IRepo<ITypeEntity> repo)
    {
        this.repo = repo;
    }
}

I get the following error:
cannot convert from IRepo <T> to IRepo <ITypeEntity>

MethodA is called with the object parameter IRepo <'DetailType>, where DetailType is inherited from ITypeEntity.

I keep thinking that this should compile, as I am restricting T in MethodA to ITypeEntity.

Any thoughts or feedback will be extremely helpful.

Thanks.

Edit: Nick R has a great suggestion, but unfortunately, in my context, I have no way to make ClassA Generic. ClassB maybe.

+5
10

. Smashery, TypeA TypeB, myType <TypeA> myType <TypeB> .

, , MethodA (myType <TypeB> b), myType <TypeB> myType <TypeA> . . , :

myType<TypeA> a; // This should be a myType<TypeB>, even if it contains only TypeA's

public void MethodB(myType<TypeB> b){ /* do stuff */ }

public void Main()
{
  MethodB(a);
}

, IREPO <ITypeEntity> MethodB, DetailTypes. . IList, :

public void MethodA<T>(IList<T> list) where T : ITypeEntity
{
  IList<T> myIList = new List<T>();

  foreach(T item in list)
  {
    myIList.Add(item);
  }

  b.MethodB(myIList);
}

, .

+3

, . . .

public interface IRepo<TRepo>
{
}

public interface ITypeEntity
{
}


public class ClassA<T> where T : ITypeEntity
{
    ClassB<T> b = new ClassB<T>();
    public void MethodA(IRepo<T> repo)
    {
        b.MethodB(repo);
    }
}
public class ClassB<T> where T : ITypeEntity
{
    IRepo<T> repo;
    public void MethodB(IRepo<T> repo)
    {
        this.repo = repo;
    }
}
+3

, . DetailType ITypeEntity, ITypeEntity. DetailType , DetailType ITypeEntity, ITypeEntity. , ...

+2

: can not IRepo <T> IRepo < 'ITypeEntity >

, IRepo<T> IRepo<ITypeEntity> - . . IRepo<T> - , T , IRepo<ITypeEntity> , T ITypeEntity.

, , T ITypeEntity.

where , , T - MethodA.

MSDN (. Generics .NET Framework), :

  • - , , , , . , Dictionary<<K, V> : . , , , .

  • , . Dictionary<K, V> , K V, .

  • , .

  • - .

  • , .

  • - . , , IComparer<T> , , . , , . , .

+1
+1

B A, , Class<B> Class<A>. , , : "T - ITypeEntity", , "IRepo<T> IRepo<ITypeEntity>". , , , .

0

T - , . , , ITypeEntity, , .

0

, , , T MethodA . , .

0

, T ITypeEntity, generics.

, , -.

0

In the context of head wrapping around common methods, let me give you a simple general function. This is the general equivalent of VB IIf () (Immediate if), which in itself is a poor imitation of a triple C-style operator (?). This is not useful for anything, since the real ternary operator is better, but perhaps it will help you understand how common functions are generated and in what contexts they should be applied.

T IIF<T>(bool Expression, T TruePart, T FalsePart)
{
    return Expression ? TruePart : FalsePart;
}
0
source

All Articles