I've read a number of articles including this one on the subject of Empty or 'marker' interfaces. I have concluded that I cannot use custom attributes in my case as I need to be able to include the instance of a class to another method and, as these classes have nothing in common, I have no option but to use a 'marker' interface.
As an example, I might have
public class Foo
{
public int Id {get;set;}
public string Name {get;set;}
}
public class Bar
{
public Guid Identifier {get;set;}
public DateTime DueDate {get;set;}
}
and I need to pass them to a method in another class and because there may be many different types that need to be passed to the method, I've defined it like this...
public void MyMethod(IMyInterface model)
{
// Do something clever here
}
And All I've had to do to make this work is to 'implement' IMyInterface
on Foo
and Bar
.
So to the question. I now find I need to call my MyMethod()
method with an anonymous type created from a LINQ statement, so I tried this ...
var data = <Some Complex LINQ>.Select(a=> new { AString = a.Value1, ADecimal = a.Value2});
MyClass.MyMethod(data);
Sadly, I get the following compile-time error:
Error 202 Cannot convert type 'AnonymousType#1' to 'IMyInterface' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
Now, I know I could create a local class and use that in the same way as I have my standard classes, but my requirements mean that I'm going to have a lot of these LINQ queries in my up-coming set of work so, if possible, I'd like to find a solution that allows me to use Anonymous Types.
Does anyone know of a solution or workaround for the error I'm getting?