Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>The idea is to be able to determine the true type of the an object in a method parameter</p> </blockquote> <p>That's easy enough (and you're already doing it).</p> <pre><code>Type actualType = param.GetType(); </code></pre> <p>That will get you the actual concrete type of the object</p> <blockquote> <p>and then cast that object as it's true type</p> </blockquote> <p>This is where things come off the rails a bit. The casting operator in C# (usage of which is what people refer to as "casting") can do two things:</p> <ol> <li>Use type-specific explicit conversions to create a new object by applying the conversion to the existing object (note that this is a <em>new</em> reference that is created; the original object's type is never changed)</li> <li>Allow the developer to reference an object as a type that is at a different level in its inheritance hierarchy than is currently provided (or an interface that is implemented on a type that is lower in the hierarchy than is currently referenced)</li> </ol> <p>In your case, the first option is right out; the casting operator, like all operators, is not polymorphic. That is, an operator is only applied if it is defined on the <em>type</em> that is being referenced, not the <em>object</em> that's being referenced. If you'd like further clarification on this, let me know, but I don't think it's germane to your question so I'm not going to go into it further unless asked.</p> <p>The second option is the only option that could realistically apply to you, but consider the only two reasons you would want to do this:</p> <ol> <li>So that you can refer to the object as a specific concrete type that is at a lower level than is currently provided (in your case, your object is an <code>object</code>, so that's pretty much as high as it goes)</li> <li>So that you can refer to an object as a type that is <em>higher</em> in the hierarchy so that you can bypass hidden (but not overridden) members.</li> </ol> <p>(The vast majority of casts are for reason #1)</p> <p>The reason you would want to use either of those options is so that you can have a strongly-typed object and use the various members defined on that type. But all of these things only apply to types that you <em>know</em> when you're writing the code. It doesn't make sense to cast to a type that is unknown at compile time, as casting doesn't do anything to the actual object (it is, and shall remain, its true type; the only thing that changes is the type of the <em>variable</em> by which you reference the object).</p> <p>If you can provide a further fleshed-out example of what you're actually trying to do (complete with code as you'd either like or expect it to work), I might be able to provide something modeled a little closer to what you want, but as it's described this is as specific as I can get.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload