Link to home
Create AccountLog in
Avatar of Michael Lam
Michael Lam

asked on

how to find out actual concrete clas when you are passed an interface

hi,

let's say i have a method Foo(IMyInterface param), and i am passing in concrete objects of types MyClass1 and MyClass2 that implements IMyInterface.  how can i tell at runtime which concrete class is being passed?  should i pass in an extra parameter that will determine the actual class, and/or use reflection?
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Michael Lam
Michael Lam

ASKER

The reason I am doing this is because I need to do serialization depending on the concrete class type.  My manager doesn't like using if statement because it's too much hardcoding.  I am thinking about passing in the concrete class name as a parameter and somehow cast the input interface object to the concrete object.
How is "passing in the concrete class name as a parameter" not hardcoding?...
yes in a sense it's still hardcoded, but you can get the class name from a config file, which gives it more flexibility.  
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account