I have the following class definition:
public interface IItem{}
public class FirstObject<T, U> : IItem
{
private readonly Expression<Func<T, U> _field;
public FirstObject(Expression<Func<T, U> field, U value)
{
_field = field;
Value = value;
}
public string FieldName
{
get
{
var member = _field.Body as MemberExpression;
if(member != null)
return member.Member.Name;
throw new Exception("exception message");
}
}
public U FieldValue { get; private set; }
}
IList<IItem> myList = new List<IItem>();
The IItem interface its only a marker to be able to put the different types of “FirstObject” into the list.
Now I need to iterate the list and get my items back. I definitely need to know both the FieldName and the FieldValue with its specific type. For this and for some other reasons I definitely need to be able to get back the objects’ original type.
Thanks for your help.
UPDATE: I think there is some confusion about what I need to do, so I updated the code and improved on the explanation.
Thank you all for trying to help.
If you want to get property ‘SomeProperty’ form all objects, that implement IItem interface, then I think you should add that property definition to IItem (It’s not just marker iterface, it states this property should exist in every object implementing it)
And implement it explicitly. This will not pollute iterface of your FirstObject objects.
And now you just get that properties by casting each item to IItem interface: