By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,992 Members | 1,344 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,992 IT Pros & Developers. It's quick & easy.

Dynamic cast with generics

P: n/a
Hi,
i'm trying to solve the next problem ...

public class BaseType {...}
public class DerivedType : BaseType { ... }

public abstract class BaseClass
{
public abstract Type getGenericType();
}

public class DerivedClass<TheType: BaseClass where TheType : BaseType
{
public override Type GetGenericType()
{
return typeof(TheType);
}
}
.. . .
public class Example
{
public static void Main()
{
. . .
DerivedClass<DerivedTypeTransferedObject = new
DerivedClass<DerivedType>();
TheCall(TransferedObject);
}
public void TheCall(BaseClass AnObject)
{
Type GenericType = AnObject.GetGenericType();

// PROBLEM... this is what i tried to .. .
TheProcess(AnObject); // Cannot because C# doesn't suppor variance
for generic types
TheProcess((TheClass<GenericType>) AnObject); // Cannot because
GenericType is a variable, not a type
}

public void TheProcess(DerivedClass<BaseTypeValue)
{
. . .
}
}

----------------
So... I need to cast a reference from an ancestor type to a derived type
dynamically.
Maybe Reflection has the solution?
Any help will be appreciated.
Néstor.
Jan 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
If you are using generics, then *use* generics. For instance:

public void TheCall<T>(T AnObject) where T : BaseClass {...}
public void TheProcess<T>(DerivedClass<TValue) where T : BaseClass
{...}

As you say, another option is reflection - but this should be a last
ditch solution. Is there a (good) reason why you can't use the above?
As the answer to this may influence the best answer...

Marc
Jan 29 '07 #2

P: n/a
....the problem is that I don't know that is the derived type used.
The scenario is the following...
I have a namespace with types and classes that defines a business model.
Then, in another namespace, I have the presentation layer where I only need
to know the base types just to show values thru polymorphism (i.e:
AnObject.ToString()).
In other words, at the caller level there is irrelevant any detailed
(derived) information, but still there is need to reference the objects...
how?.
Thanks,

Néstor.
"Marc Gravell" <ma**********@gmail.comescribió en el mensaje
news:Ow**************@TK2MSFTNGP03.phx.gbl...
If you are using generics, then *use* generics. For instance:

public void TheCall<T>(T AnObject) where T : BaseClass {...}
public void TheProcess<T>(DerivedClass<TValue) where T : BaseClass {...}

As you say, another option is reflection - but this should be a last ditch
solution. Is there a (good) reason why you can't use the above? As the
answer to this may influence the best answer...

Marc

Jan 29 '07 #3

P: n/a

Then yes, reflection may be your best bet. In this scenario I would
use GetType() and MakeGenericMethod to invoke the method (as above) so
that my reflection is limited to the invoke step, rather than
throughout.

Watch for wrap: <http://msdn2.microsoft.com/en-us/library/
system.reflection.methodinfo.makegenericmethod(VS. 80).aspx>

Marc

Jan 29 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.