Re: Dynamic cast with generics

Tech-Archive recommends: Speed Up your PC by fixing your registry



....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" <marc.gravell@xxxxxxxxx> escribió en el mensaje
news:OwSpsg3QHHA.3444@xxxxxxxxxxxxxxxxxxxxxxx
If you are using generics, then *use* generics. For instance:

public void TheCall<T>(T AnObject) where T : BaseClass {...}
public void TheProcess<T>(DerivedClass<T> Value) 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



.



Relevant Pages

  • XSD type derived via restriction in same namespace as restricted type?
    ... to the base type, it follows that the namespace of the derived type ...
    (comp.text.xml)
  • Re: Class Variable Access and Assignment
    ... >>> b.a would refer to the same object. ... > will be resolved to two different namespace I don't see the relevance. ... > resolves to different namespaces. ...
    (comp.lang.python)
  • Re: ANN: threads enabled Itcl
    ... scenario. ... Object deletion and thread exiting was indeed a tricky point and I ... when the access commands are transferred via ... I would assume you may need to create the parent namespace in each other ...
    (comp.lang.tcl)
  • Multiple Projects
    ... I have a .Net solution that contains multiple projects of both Managed ... Each projects uses its own namespace, ... I cannot get this scenario to compile. ... Both projects are mixed mode and reference common unmanaged static C++ ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Multiple Projects
    ... I could reproduce this with a test application and a control class. ... removing the reference from your project, ... > namespace line referring to the namespace declared in the Proj1 classes ... > I cannot get this scenario to compile. ...
    (microsoft.public.dotnet.languages.vc)