Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How to perform 'as' operation with a Type

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?

like image 272
IEHaterNumber1 Avatar asked Sep 21 '11 18:09

IEHaterNumber1


People also ask

What C is used for?

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 ...

What is the full name of C?

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.

Is C language easy?

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 in C language?

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.


3 Answers

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) {
        ...
    }
    ...
}
like image 100
mellamokb Avatar answered Oct 05 '22 02:10

mellamokb


...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.

like image 41
Henk Holterman Avatar answered Oct 05 '22 04:10

Henk Holterman


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);
}
like image 28
Pankaj Upadhyay Avatar answered Oct 05 '22 02:10

Pankaj Upadhyay