0

I Have code like bellow

public interface IFoo
{
    int One { get; set; }

    string Two { get; set; }
}

public class Foo : IFoo
{
    public int One { get; set; }
    public string Two { get; set; }
}

public class SomeDto<T>
    where T : IFoo
{
    public T Fik { get; set; }

    public string Faj { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var fik = new SomeDto<Foo>() { Fik = new Foo() { One = 1, Two = "ss" }, Faj = "ss" };

        var obj = (Object)fik;

        // the problematic part 
        //=======================
        var fikl = fik as SomeDto<IFoo>;  //do something like that 
                                          //or
        var fikl = (SomeDto<IFoo>)fik;   //do something like that 
    }
}

How can I work around that problematic part. Make something similar to allow unboxing a generic class but to something more general like interface.

I need this because I have combo box with datasource build upon generics. And I need to find item on the list using to that some properties form generic value.

user3333010
  • 113
  • 2
  • 9

1 Answers1

3

1) There is nothing about boxing/unboxing

2) You can't do that because SomeDto<IFoo> and SomeDto<Foo> are invariant to each other. That means even if "IFoo is assignable from Foo" you can't say "SomeDto<IFoo> is assignable from SomeDto<Foo>" and vice versa.

Something similiar to your intentions is using covariant/contravariant generic interfaces (because only delegates and interfaces support that trick). For example:

    interface IFoo
    {
        int Int { get; set; } 
    }

    class Foo : IFoo
    {
        public int Int { get; set; }
    }

    interface IDto<out T> where T : IFoo
    {
        T Obj { get; }
    }

    class Dto<T> : IDto<T> where T : IFoo
    {
        public T Obj { get; set; }
    }

so you can assign IDto<Foo> to IDto<IFoo> like that:

    static void Main(string[] args)
    {
        Foo foo = new Foo();
        IDto<IFoo> dto = new Dto<IFoo>() {Obj = foo};
        object obj = dto;
        dto = (IDto<Foo>) obj;
    }
Kovpaev Alexey
  • 1,725
  • 6
  • 19
  • 38
  • Covariant in this case, not contravariant. It would be worth mentioning that the interface is necessary because classes can't be co- or contravariant, only interfaces and delegates can. – Thomas Levesque Aug 06 '16 at 00:34
  • @ThomasLevesque it's only example because I don't know what the author need. But I will mention about necessity of using interfaces if it's not obvious from my answer, thanks. – Kovpaev Alexey Aug 06 '16 at 10:26
  • Sorry for my mistake with my bad understanding the boxing and unboxing thing. Also thanks for the answer. Unfortunately it still doesn't solve my problem. I wanted to cast for interface (IFoo) because I didn't know the exact implementation in that place. I know that the object is of type IFoo ant that is all. IFoo have more than one implementation. At the end I've solved that problem by changing data that I put to the data source for my combobox. Thanks for all answers. – user3333010 Aug 09 '16 at 15:20