I want to test whether a given object
can be cast to a given Type
.
In this scenario, I have an object, and the Type
representing what type I want to cast it to:
public function FooBar(..., object data, Type expected) {
...
var unboxedData = ?
if (unboxedData == null) {
....
}
...
}
How can I cast data
to the type type
represents?
Basically, I want to do this:
var unboxedData = data as Type;
...but of course you can't use Type
with an as
statement, so what do I do?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Edit 2: I'm going to say it's not possible without reflection or generics. With reflection, you have no compile-time checking and must use reflection (or dynamic
) to further call methods/properties of the object. With generics, you can't use a Type object to get there alone. Take your pick. Is it possible to refactor your calling code to allow generics?
If allowed, this may be more easily handled with a generic method:
public resultType FooBar<T>(..., object data) {
...
T unboxedData = (T)data;
...
}
Edit: Also, you can use data as T
if you include a generic type constraint of where T : class
:
public something FooBar<T>(..., object data)
where T : class
{
...
T unboxedData = data as T;
if (unboxedData == null) {
...
}
...
}
...but of course you can't use Type with an as statement, so what do I do?
Morre importantly, you can't use var
this way. So there is nothing to be gained here.
You can test if it's the right type with
if (expected.IsInstanceOfType(data))
But then you still can't write any decent code to access properties or methods on data
.
C# provides the as keyword to quickly determine at runtime whether a given type is compatible with another. When you use the as keyword, you are able to determine compatibility by checking against a null return value. Consider the following:
Hexagon hex2 = frank as Hexagon;
if (hex2 == null)
Console.WriteLine("Sorry, frank is not a Hexagon...");
In addition to the as keyword, the C# language provides the is keyword to determine whether two items are compatible. Unlike the as keyword, however, the is keyword returns false, rather than a null reference, if the types are incompatible.
if (emp is SalesPerson)
{
Console.WriteLine("{0} made {1} sale(s)!", emp.Name,
((SalesPerson)emp).SalesNumber);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With