Casting an object to a generic interface?

If I understand the question, then the most common approach would be to declare a non-generic base-interface, i. E internal interface IRelativeTo { object getRelativeTo(); // or maybe something else non-generic void setRelativeTo(object relativeTo); } internal interface IRelativeTo : IRelativeTo where T : IObject { new T getRelativeTo(); new void setRelativeTo(T relativeTo); } Another option is for you to code largely in generics... i.e. You have methods like void DoSomething() where T : IObject { IRelativeTo foo = // etc } If the IRelativeTo(foo) There are benefits to both approaches.

If I understand the question, then the most common approach would be to declare a non-generic base-interface, i.e. Internal interface IRelativeTo { object getRelativeTo(); // or maybe something else non-generic void setRelativeTo(object relativeTo); } internal interface IRelativeTo : IRelativeTo where T : IObject { new T getRelativeTo(); new void setRelativeTo(T relativeTo); } Another option is for you to code largely in generics... i.e. You have methods like void DoSomething() where T : IObject { IRelativeTo foo = // etc } If the IRelativeTo is an argument to DoSomething(), then usually you don't need to specify the generic type argument yourself - the compiler will infer it - i.e.

DoSomething(foo); rather than DoSomething(foo); There are benefits to both approaches.

Unfortunately inheritance doesn't work with generics. If your function expects IRelativeTo, you can make the function generic as well: void MyFunction(IRelativeTo sth) where T : IObject {} If I remember correctly, when you use the function above you don't even need to specify the type, the compiler should figure it out based on the argument you supply. If you want to keep a reference to one of these IRelativeTo objects inside a class or method (and you don't care what T is that), you need to make this class/method generic again.

I agree, it is a bit of pain.

If all you care about is that IRelativeTo deals with IObjects then you don't need to make it generic: interface IRelativeTo { IObject getRelativeTo(); void setRelativeTo(IObject relativeTo) } The implementing classes may still be generic, however: abstract class RelativeTo : IRelativeTo where T : IObject { public virtual T getRelativeTo() {return default(T);} public virtual void setRelativeTo(T relativeTo) {} IObject IRelativeTo.getRelativeTo() {return this.getRelativeTo(); } void IRelativeTo. SetRelativeTo(IObject relativeTo) { this. SetRelativeTo((T) relativeTo); } } class AdminRateShift : RelativeTo, IObject {} Then you can do this: IRelativeTo irt = new AdminRateShift(); IObject o = irt.getRelativeTo(); irt.

SetRelativeTo(o).

If I understand the question, then the most common approach would be to declare a non-generic base-interface, i.e. Another option is for you to code largely in generics... i.e. If the IRelativeTo is an argument to DoSomething(), then usually you don't need to specify the generic type argument yourself - the compiler will infer it - i.e.

There are benefits to both approaches.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions