1

I have currently a problem

I have 1 Interface with two types of argument like this

ITestInterface<ArgumentA>
ITestInterface<ArgumentB>

this interface has only the argument as different

I would like to pass this interface to an constructor of a class. sth like this

public class MyClass
{
    public ITestInterface<object> MyInterface {get; set;}

    public MyClass(ITestInterface<ArgumentA> testInterfaceA){
    this.MyInterface = testInterfaceA as ITestInterface<object>;
    this.MyTestInterface.SomeEvent += this.OnSubcribe;
    }
    
    public MyClass(ITestInterface<ArgumentB> testInterfaceB){
    this.MyInterface = testInterfaceB as ITestInterface<object>;
    this.MyTestInterface.SomeEvent += this.OnSubcribe;
    }

public void OnSubcribe(){
//Work to do here, dont care about what argument the interface has.
}
}

and to call the MyClass constructor I have sth like this:

public List<MyClass> ClassList = new();
    
public void testMethod(){
var interA = getInterfaceWithArgumentA();
var myClassA = new MyClass(interA);

var interB = getInterfaceWithArgumentB();
var myClassB = new MyClass(interB);
}

So the problem is i am not able to cast the interface argument to object. I dont need to differenciate the argument either. I just want to avoid to have 2 properties of MyInterface like (MyInterfaceA, MyInterfaceB).

I need also to consider that maybe in the future I will have more type of Argument so maybe to have multiple properties like MyInterfaceA, MyInterfaceB, MyInterfaceC and also multiple constructor for each Interfaceargument type would be a mess.

I just thought about have a Baseclass and the ArgumentA and ArgumentB class derived from it so the cast would work but its not like that.

How would I solve this problem ?

Many Thanks

user7112196
  • 87
  • 2
  • 9

1 Answers1

3

I think you have not provided what getInterfaceWithArgumentB() and getInterfaceWithArgumentA() method doing. I am making few assumption.

To solve your problem Generic will help.

Following is the example of it.

 public class MyClass<T>
{
    public ITestInterface<T> MyInterface { get; set; }

    public MyClass(ITestInterface<T> testInterfaceA)
    {
        this.MyInterface = testInterfaceA;
        this.MyInterface.SomeEvent += MyInterface_SomeEvent;
    }
    private void MyInterface_SomeEvent(object sender, EventArgs e)
    {
        Console.WriteLine(sender);
    }
    
}

public class ArgumentA
{
    public string Name { get; set; }
}

public class ArgumentB
{
    public int Id { get; set; }
}

public interface ITestInterface<T>
{
    T Data { get; set; }

    event EventHandler SomeEvent;
    void OnSomeEvent();
}

public class TestInterface<T> : ITestInterface<T>
{
    public T Data { get ; set; }

    public event EventHandler SomeEvent;

    public void OnSomeEvent()
    {
        if(SomeEvent != null)
        SomeEvent(Data, EventArgs.Empty);
    }
}

You can use it like following.

MyClass<ArgumentA> myClass = new MyClass<ArgumentA>(
                                             new TestInterface<ArgumentA>()
                                             {
                                                 Data = new ArgumentA() { Name = "test" }
                                             });
            MyClass<ArgumentB> myClas2 = new MyClass<ArgumentB>(
                                             new TestInterface<ArgumentB>() 
                                             { 
                                                 Data = new ArgumentB() { Id = 10 } 
                                             });

            myClas2.MyInterface.OnSomeEvent();
            myClass.MyInterface.OnSomeEvent();

UPDATE

 class Program
{
    static void Main(string[] args)
    {
       
        ObservableCollection<MyClass> items = new ObservableCollection<MyClass>();
        
        MyClass<ArgumentA> myClass = new MyClass<ArgumentA>(
                                         new TestInterface<ArgumentA>()
                                         {
                                             Data = new ArgumentA() { Name = "test" }
                                         });
        MyClass<ArgumentB> myClas2 = new MyClass<ArgumentB>(
                                         new TestInterface<ArgumentB>() 
                                         { 
                                             Data = new ArgumentB() { Id = 10 } 
                                         });
        items.Add(myClass);
        items.Add(myClas2);
     
        myClas2.MyInterface.OnSomeEvent();
        myClass.MyInterface.OnSomeEvent();

    }
}

public class MyClass
{
    public ITestInterface MyInterface { get; set; }
}

public class MyClass<T> : MyClass
{
    public MyClass(ITestInterface<T> testInterfaceA)
    {
        this.MyInterface = testInterfaceA;
        this.MyInterface.SomeEvent += MyInterface_SomeEvent;
    }
    private void MyInterface_SomeEvent(object sender, EventArgs e)
    {
        Console.WriteLine(sender);
    }

}

public class ArgumentA 
{
    public string Name { get; set; }
}

public class ArgumentB 
{
    public int Id { get; set; }
}
public interface ITestInterface
{
    event EventHandler SomeEvent;
    void OnSomeEvent();
}
public interface ITestInterface<T> : ITestInterface
{
    T Data { get; }
}

public class TestInterface<T> : ITestInterface<T> 
{
    public T Data { get; set; }

    public event EventHandler SomeEvent;

    public void OnSomeEvent()
    {
        if (SomeEvent != null)
            SomeEvent(Data, EventArgs.Empty);
    }
}
dotnetstep
  • 17,065
  • 5
  • 54
  • 72
  • I understand your solution. I have done the same but with that I will have a problem if I want to keep the classInstance in a observablecollection. The part to create MyClass Instance is in ViewModel and in there I just want to have one ObservableCollection>. If I do it like your solution with generic, I somehow need to create 2 observablecollection for each type of myclass and in case I have in the future multiple argument, i would need to have multiple observablecollection for each argumentyp. The goal is to have 1 observablecollection for all type of instance of myclass – user7112196 Mar 20 '21 at 16:41
  • I think you have not described your observablecollection related things earlier. – dotnetstep Mar 20 '21 at 17:14
  • Perfect, thats the solution . – user7112196 Mar 20 '21 at 18:09