Could you elaborate which part is diffucult to follow?
Well, for one...because the scenario you describe seems so remarkably
broken, it's difficult to comprehend how it came up at all. The code
example you provided, offers no insight whatsoever as to why what you're
asking to do is in any way a reasonable, technically appropriate thing to
do. In addition, the question is asked so broadly, it opens the
possibility of someone changing "SomeProperty" from a bool to a string
(for example), begging the question as to what it would even mean to
perform the cast.
I'm looking for a good way to typecast an object to a corresponding
type of the variable i'm going to assign it to. Is it (easily) doable?
No, not without modifying the code according to the change in type. The
whole point of declaring something a particular type is so that you get
_compile-time_ verification of the type.
If you only want run-time verification, then one approach is to not bother
using typed members in the first place. Just make "SomeProperty" a
System.Object, and let boxing deal with conversion of value types.
Presumably where the property is actually consumed, you still need to know
the type and it can be cast as necessary there.
Other alternatives might include changing the design so that properties of
this nature are implemented more explicitly, with methods available to set
the property value. Provide overloads for each anticipated input type,
and write explicit conversion code in the method to handle the
conversion. IMHO, this is the most reliable, because it makes it very
clear what the rules for conversion of a type are, rather than your code
just doing a cast and hoping for the best.
Yet another option is to use the Convert class. Assuming the types you're
using are all limited to those supported by the Convert class, _and_ the
exact conversion provided by the Convert class are in fact the ones that
are appropriate for your situation, that could work (it's basically a
variation on the suggestion in the previous paragraph).
[...]
I'm looking for a good way to deal with the situation, though, once i've
found myself facing it.
Honestly, faced with the situation, IMHO your best possible solution is
either to fire the person who created the situation, or look for a job in
a place where that kind of person doesn't exist.
Pete