I am trying to create an object type (MyObject) with a linq expression of type T. My class states that the value of T must be of type BaseModel (which is an object created by me). Below is how MyObject is constructed:
public class MyObject<T> where T : BaseModel
{
public Type MyType;
public Expression<Func<T, bool>> MyExpression;
}
All of my models inherit from BaseModel. Example:
public class MyModel : BaseModel
{
public string Name { get; set; }
}
My object is used inside of a generic static class:
public static class MyStaticClass
{
private static Dictionary<MyObject<BaseModel>, string> MyDictionary = new Dictionary<MyObject<BaseModel>, string>();
public static AddMyObjectsToDictionary(List<MyObject<BaseModel>> myObjects)
{
//CODE
}
//REST OF CODE
}
Then when my app loads it does the following (Error is thrown here):
List<MyObject<BaseModel>> myObjects = new List<MyObject<BaseModel>>();
myObjects.Add(new MyObject<MyModel>()
{
MyType = typeof(MyModel),
MyExpression = p => p.Name == "myname"
});
MyStaticClass.AddMyObjectsToDictionary(myObjects);
Exact error message thrown with the namespaces to show in which project each object is located:
cannot convert from
'ProjectANamespace.MyObject<ProjectBNamespace.MyModel>'
to'ProjectANamespace.MyObject<ProjectANamespace.BaseModel>'
I need to be able to create a generic expression within MyModel however I cannot specify MyModel inside of MyStaticClass since it is meant to be a generic class which is located in another project along with BaseModel.
Anyone have any ideas how resolve this issue?