views:

311

answers:

1

Okay, I have a an interface, lets call it

public interface bar {
     string Foo;
}

also have a class that implements the interface

public fooBar : bar {
   public string Foo {get; set;}
}

I then have a property hanging off another object that contains a list of interface "bar" that contain different implementations, like so,

public list<bar> listOfBars;

Now, when I use an expression tree/function like so

function(parentObj x) { x.listOfBars(0).Foo;}

I can get the memberinfo off of the expression tree. The memberinfo though is pointing to the interface method and its class is the interface. That almost works, but I need to know what its parent class is, so I would need from the memberinfo object to find and see its calling a method off of the "bar" interface, and the class type is "fooBar". Is there a way to do this, I've dug through the memberinfo object in the watch window and can't get to the parent type. I might be missing something though.

A: 

I think you are getting some concepts confused. When you implement an interface, you are not actually deriving your class from the interface. You are actually doing this:

public class fooBar : object, Foo { }

You class will derive from an object and also implement the interface Foo. Classes can only derive from one object type, but can implement multiple interfaces.

Another problem you will have is that interfaces cannot contain fields. You need to change this:

public interface bar {string Foo;}

to this

public interface bar {string Foo {get;set;} }

Now, assuming you change all of these items, you might want to look into the IS keyword. You can check to see if a class implements an interface by doing the following:

object o = new object();
if(o is bar) {
  //do interface bar stuff here
}
Jason Z
Sorry, most of the code is in VB, and when i typed out the C# I may of not had the syntax all the way right. The interface is setup like you saidpublic interface bar {string Foo {get;set;} }Also the IS keyword will not work since I don't want to have a case statement of all of my classes. When I grab the member from reflection like sopropertyMethod = TryCast(expression.Body, System.Linq.Expressions.MemberExpression)It only tells me about the interface and the interface property the tree points to. I can't get to the objects real class name or nothing.
Josh