I am getting an object of type Company<IDesignation>
. Now I want to cast it to ICompany<Manager>
. Run time I know that IDesignation
is nothing but "Manager" type.

- 235,767
- 35
- 427
- 472

- 281
- 2
- 10
-
Correcting as I see some issues typing angle brackets......Concrete type of Company holding interface type of IDesignation to Interface type of ICompany holding concrete type of Manager in C# (Cast this Company'
' to ICompany' – crazy coding Mar 12 '17 at 08:06') in C# -
1Well what happens if you try? Please post a [mcve] that shows what the problem is. – Lasse V. Karlsen Mar 12 '17 at 08:13
-
Ahh, I see. Sounds like you may want to rework this classes inheritance. It sounds like you are in the beginning stages of code smell, but I could be wrong. I dont see how youre going to cast it – yardpenalty.com Mar 12 '17 at 08:16
-
1Please consider sharing a simple code snippet, otherwise is hard to figure out the relationship between the components – vvv Mar 12 '17 at 08:32
-
Please see my example. I think im guessing what u r looking for? – yardpenalty.com Mar 12 '17 at 08:48
2 Answers
Is this what you are looking for?
Company comp = new Company();
Manager mgner = new Manager(comp.getManager());
IDesignation manager = mgner;
ICompany company = (ICompany)manager;
Assuming Company is:
public class Company: ICompany, IDesignation //or something?
Using either Generic Type Casting (what u are trying to do) or simply Casting an Interface or object depends on whether you will perform this task explicitly or implicitly (maybe class has pre-defined function to cast it) and as your comment has pointed out... maybe by user or runtime matters or whether or not and/or how you need to instantiate your object so I would really need to see the class implementation in order to be able to provide something that uses type casting in the way you want to perform it.

- 1,244
- 2
- 17
- 32
-
public interface IEntityDelta
: IEntityDelta where T : IEntity { } public interface IEntityDelta { //Set of properties } public class EntityDelta – crazy coding Mar 12 '17 at 09:58: IEntityDelta where T : class, IEntity { //Concrete implementation of IEntityDelta } public abstract class Entity : IEntity { //abstract implementation of IEntity } public class Order : Entity { //Concrete implementation } I get the type EntityDelta // IEntity is of type Order I need to cast it to IEntityDelta and pass it to another service
What you need is Contravariance
, i.e. your IEntityDelta
generic type parameter needs to be made contravariant.
The only way to do that and for this to work is to have:
public interface IEntityDelta<in T> : IEntityDelta where T : IEntity
Note the in T in the definition.
Check out the in (Generic Modifier) (C# Reference)
Or this Understanding Covariant and Contravariant interfaces in C#
If you're not the creator of that interface and if the IEntityDelta<>
is defined w/o the in
modifier you're out of luck.
And just to mention that adding in/out modifiers is easier said than done. For that to compile your methods, properties etc. need to satisfy the conditions of contravariance (or covariance in case of 'out') on that generic type (T) parameter.
And this what your classes, interfaces look like based on your info (which was terrible btw. next time you need to dedicate a bit more time in providing minimal but complete code that makes sense):
public interface IEntityDelta<in T> : IEntityDelta
where T : IEntity
{
void MakeDelta(T entity); // this is allowed
//T Entity { get; set; } // this won't work
}
public class EntityDelta<T> : IEntityDelta<T>
where T : class, IEntity
{
public T Entity { get; set; }
public EntityDelta(T entity) => Entity = entity;
public void MakeDelta(T entity) { }
}
public interface IEntityDelta { }
public abstract class Entity : IEntity { }
public class Order : Entity { }
public interface IEntity { }
...and usage:
var order = new Order();
EntityDelta<IEntity> orderDelta = new EntityDelta<IEntity>(order);
IEntityDelta<IEntity> idelta = orderDelta;
IEntityDelta<Order> iOrderDelta = orderDelta;

- 1
- 1

- 14,052
- 3
- 41
- 51