1

I need to create a function that takes a List and 2 property names of T and return a list meeting the requirements of a specified interface plus any other properties of T.

Eg.

interface ITest
{
  int ID {get;set};
  int ParentID {get;set;};
}

Consumer has any class that is convertible into this interface but with potentially different properties than is required by the interface:

public class ConsumingClass
{
  public int ConsumingClassID {get;set;
  public int ConsumingClassParentID {get;set;}
  public string namex {get;set;}
  public string namey {get;set;}
}

So the consumer needs to get an object that meets the requirements of the interface, so he wants to call a function such as:

List<ITest> getnewlist = ConvertList<ConsumingClass>(consuminglist, "ConsumingClassID", "ConsumingClassParentID");

The signature of the method would look like this:

public List<ITest> ConvertList<T>(List<T> ListBeingConverted, string IDAlias, string ParentIDAlias)
{
    //code needs to return a list meeting ITest requirements plus any other properties contained in T
}

Edit: I modified the signature to more appropriately reflect what I'm looking to return.

apierceSO
  • 165
  • 1
  • 13
  • What's the question? – MarcinJuraszek Aug 30 '14 at 03:04
  • Why just not inherit `ConsumingClass` from `ITest`????? – Tony Aug 30 '14 at 03:15
  • 1. ITest or List>? 2. Need string names? Yes: reflection would be needed, No: Delegates or LINQ/Lambdas could be used. – firda Aug 30 '14 at 08:07
  • @MarcinJuraszek the question is what code can I add to solve my problem? I want to return a new List object conforming to my ITest interface regardless of what T looks like. I'm thinking anonymous types or ExpandoObjects. Clearly reflection is required. – apierceSO Aug 30 '14 at 13:56
  • @Tony, I could do this but then I'd have to do this for every class I want to convert, which defeats the purpose of what I'm trying to accomplish: which is to create a method to convert a list of objects having certain properties to a list of objects meeting the interface criteria – apierceSO Aug 30 '14 at 13:58

1 Answers1

1

Derive from ITest to create a class that adapts an instance of ConsumingClass to conform to the ITest interface:

class AnyObjectAsITest : ITest {
 public object Object;
 public string PropertyName;

 public int ID {
  get {
   return (int)Object.GetType().GetProperty(PropertyName).GetValue(Object, null);
  }
 }
 //...
}
usr
  • 168,620
  • 35
  • 240
  • 369
  • I'm not quite seeing this but maybe it can be fleshed out...how would I may AnyObjectAsITest contain all of the other properties of Object dynamically? – apierceSO Aug 30 '14 at 14:00
  • You need to implement *all* interface methods. I just implemented one for demonstration purposes. Also, you need to set the two fields. – usr Aug 30 '14 at 14:05
  • Setting the 2 fields, I understand. How to take namex and namey in my example to new properties called "namex" and "namey" in this class also needs to be done and I don't see that in your code. Maybe you can show an example of how it would be implemented. – apierceSO Aug 30 '14 at 14:12
  • Well, if the interface does not have those properties, how could a caller access them trough the interface? That's impossible. – usr Aug 30 '14 at 14:17
  • Right, I'm looking for a way to dynamically create a new object meeting the requirements of the interface regardless of the original type details. By doing this, I could convert any object into a new object having the interface requirements, which will allow me to act on it for view purposes. – apierceSO Aug 30 '14 at 14:22
  • I showed you how to meet the requirements of the interface. I'm not sure I understand how this does not meet your requirements. The interface is implemented and works. – usr Aug 30 '14 at 14:25
  • This link shows a bit what I'm trying to do. http://stackoverflow.com/questions/191013/can-a-c-sharp-anonymous-class-implement-an-interface?rq=1 – apierceSO Aug 30 '14 at 14:36