musademir
musademir
CC#
Created by musademir on 12/19/2022 in #help
❔ Generic class to fire on data change according to data type
Hi everyone. I am tring to implement generic class that holds generic type of data class. And I will store them in a dictionary and fire their function according to type of data class. Please check code for more detail. Here is my interfaces
public interface IMyDataInterface
{
string SomeData { get; set; }
}

public interface IMyDataListener<TData> where TData : IMyDataInterface
{
void OnDataChange(TData data);
}
public interface IMyDataInterface
{
string SomeData { get; set; }
}

public interface IMyDataListener<TData> where TData : IMyDataInterface
{
void OnDataChange(TData data);
}
Classes that inherits them
public class MyData : IMyDataInterface
{
public string SomeData { get; set; }
public string SomeNewData1 { get; set; }
}

public class MyDataListener : IMyDataListener<MyData>
{
public void OnDataChange(MyData data)
{
}
}

public class MyData2 : IMyDataInterface
{
public string SomeData { get; set; }
public string SomeNewData2 { get; set; }
}

public class MyData2Listener : IMyDataListener<MyData2>
{
public void OnDataChange(MyData2 data)
{
}
}
public class MyData : IMyDataInterface
{
public string SomeData { get; set; }
public string SomeNewData1 { get; set; }
}

public class MyDataListener : IMyDataListener<MyData>
{
public void OnDataChange(MyData data)
{
}
}

public class MyData2 : IMyDataInterface
{
public string SomeData { get; set; }
public string SomeNewData2 { get; set; }
}

public class MyData2Listener : IMyDataListener<MyData2>
{
public void OnDataChange(MyData2 data)
{
}
}
Then I want to add them to a dictionary and call according to their data type.
public class MyDataListenerManager
{
private Dictionary<Type, IMyDataListener<IMyDataInterface>> listeners = new()
{
{typeof(MyData), new MyDataListener()},//Error: Argument type 'MyDataListener' is not assignable to parameter type 'IMyDataListener<IMyDataInterface>'
{typeof(MyData2), new MyData2Listener()} // Error: Argument type 'MyDataListener2' is not assignable to parameter type 'IMyDataListener<IMyDataInterface>'
};

//call from somewhere else
public void OnDataChange(IMyDataInterface data)
{
if (listeners.TryGetValue(data.GetType(), out var listener))
{
listener.OnDataChange(data);
}
}
}
public class MyDataListenerManager
{
private Dictionary<Type, IMyDataListener<IMyDataInterface>> listeners = new()
{
{typeof(MyData), new MyDataListener()},//Error: Argument type 'MyDataListener' is not assignable to parameter type 'IMyDataListener<IMyDataInterface>'
{typeof(MyData2), new MyData2Listener()} // Error: Argument type 'MyDataListener2' is not assignable to parameter type 'IMyDataListener<IMyDataInterface>'
};

//call from somewhere else
public void OnDataChange(IMyDataInterface data)
{
if (listeners.TryGetValue(data.GetType(), out var listener))
{
listener.OnDataChange(data);
}
}
}
Eventhough MyData and MyData2 inherits IMyDataInterface, it gives me not assignable to parameter type error. And here is the purpose of that implementation. It should trigger related listener's OnDataChange function
public class TestClass
{
MyDataListenerManager manager = new();

public void Test()
{
manager.OnDataChange(new MyData()
{
SomeData = "test",
SomeNewData1 = "test1"
});
}

public void Test2()
{
manager.OnDataChange(new MyData2()
{
SomeData = "test2",
SomeNewData2 = "test22"
});
}
}
public class TestClass
{
MyDataListenerManager manager = new();

public void Test()
{
manager.OnDataChange(new MyData()
{
SomeData = "test",
SomeNewData1 = "test1"
});
}

public void Test2()
{
manager.OnDataChange(new MyData2()
{
SomeData = "test2",
SomeNewData2 = "test22"
});
}
}
Can you please guide me about how should I implement that, or what is wrong with the code?
67 replies