views:

624

answers:

4

I have a object:

ExampleClass ex = new ExampleClass();

And:

Type TargetType

I would like to cast ex to type described by TargetType like this:

Object o = (TargetType) ex;

But when I do this I get:

The type or namespace name 't' could not be found

So how to do this? Am I missing something obious here?

Update:

I would like to obtain something like this:

public CustomClass MyClassOuter
{
   get
   {
        return (CustomClass) otherClass;
   }
}

private otherClass;

And because I will have many properties like this I would like do this:

public CustomClass MyClassOuter
{
   get
   {
        return (GetThisPropertyType()) otherClass;
   }
}

private SomeOtherTypeClass otherClass;

Context:

Normally in my context in my class I need to create many properties. And in every one replace casting to the type of property. It does not seem to have sense to me (in my context) because I know what return type is and I would like to write some kind of code that will do the casting for me. Maybe it's case of generics, I don't know yet.

It's like I can assure in this property that I get the right object and in right type and am 100% able to cast it to the property type.

All I need to do this so that I do not need to specify in every one property that it has to "cast value to CustomClass", I would like to do something like "cast value to the same class as this property is".

For example:

class MYBaseClass
{
   protected List<Object> MyInternalObjects;
}

class MyClass
{
   public SpecialClass MyVeryOwnSpecialObject
   {
      get
      {
           return (SpecialClass) MyInteralObjects["MyVeryOwnSpecialObject"];
      }
   }
}

And ok - I can make many properties like this one above - but there is 2 problems:

1) I need to specify name of object on MyInternalObjects but it's the same like property name. This I solved with System.Reflection.MethodBase.GetCurrentMethod().Name.

2) In every property I need to cast object from MyInternalObjects to different types. In MyVeryOwnSpecialObject for example - to SpecialClass. It's always the same class as the property.

That's why I would like to do something like this:

class MYBaseClass
{
   protected List<Object> MyInternalObjects;
}

class MyClass
{
   public SpecialClass MyVeryOwnSpecialObject
   {
      get
      {
           return (GetPropertyType()) MyInteralObjects[System.Reflection.MethodBase.GetCurrentMethod().Name];
      }
   }
}

And now concerns: Ok, what for? Because further in my application I will have all benefits of safe types and so on (intellisense).

Second one: but now you will lost type safety in this place? No. Because I'm very sure that I have object of my type on a list.

+3  A: 
Object o = (TargetType) ex;

This code is useless. You might have a type on the right but it's still only an object on the left side. You can't use functionality specific to TargetType like this.


This is how you can invoke a method of an unknown object of a given type:

object myObject = new UnknownType();
Type t = typeof(UnknownType); // myObject.GetType() would also work
MethodInfo sayHelloMethod = t.GetMethod("SayHello");
sayHelloMethod.Invoke(myObject, null);

With this UnknownType class:

class UnknownType
{
    public void SayHello()
    {
        Console.WriteLine("Hello world.");
    }
}
I need to cast ex to type my custom 'TargetType'. What do you propose to get this done?
tomaszs
You'll have to invoke methods (or whatever you want to do) via reflection. I'll try to find an example.
i've updated question to show context
tomaszs
Your context code still has the same problem I described. You try to cast something to (GetThisPropertyType()) but in the end you return a CustomClass instance anyway.
A: 
if (ex is ExampleClass) 
{
  ExampleClass myObject = (ExampleClass)ex;
}

That would do it but I guess the question is what are you trying to achieve and why? I often find that if something seems really, really difficult then I'm probably doing it wrong.

Lazarus
i've updated question to show context
tomaszs
A: 

Usually a desire to do this indicates a misunderstanding. However, there are very occasionally legitimate reasons to do this. It depends on whether or not it's going to be a reference conversion vs an unboxing or user-defined conversion.

If it's a reference conversion, that means the actual value (the reference) will remain entirely unchanged. All the cast does is perform a check and then copy the value. That has no real use - you can perform the check using Type.IsAssignableFrom instead, and just use the implicit cast to object if you want it in a variable of type object.

The main point of casting is to provide the compiler with more information. Now if you only know the type at execution time, that clearly can't help the compiler.

What do you plan to do with o after you've performed the cast? If you can explain that, we can try to explain how to achieve the effect you're after.

If you really want a user-defined conversion or an unboxing conversion to occur, that could be a different matter - but I suspect that's not the case.

EDIT: Having seen your update, your property is just declared to return CustomClass, so all you need is:

public CustomClass MyClassOuter
{
   get
   {
        return (CustomClass) otherClass;
   }
}

I suspect you still haven't really given us all the information we need. Is that definitely how your property will be defined, with CustomClass being a definite type? Why were you trying to perform the cast dynamically when you know the property type?

EDIT: It sounds like you're just trying to save some typing - to make it easier to cut and paste. Don't. You know the type at compile-time, because you know the type of the property at compile-time (it's specified in the code just a few lines above). Use that type directly. Likewise don't try to get the name of the current method to work out the key to use. Just put the name in directly. Again, you know it at compile-time, so why be dynamic? I'm all for laziness when it makes sense, but in this case it just doesn't.

Jon Skeet
i've updated question to show context
tomaszs
I've updated my answer to respond.
Jon Skeet
I may be missing the point here but it looks like he's trying to craft a generic 'get' for the properties in any class. Given that the properties isn't named I don't see how this could work but I'm happy to be educated here.
Lazarus
@Lazarus: This is why I've asked for a complete example. I suspect that when we get to see more, it'll be clearer what's going on. It's possible that using generics would help, but we can't tell from the limited information we've got.
Jon Skeet
I need to do casting without specify the cast class direct. Inside property it's known what type it is. I would like to cast otherClass to this class type without specifying it directly because in this context it's obvious.
tomaszs
Where do you think the benefit is in casting to any type other than the type of the property itself? If you *really* think you have a good reason to do this, please post a *complete* example.
Jon Skeet
@tomaszs: You can't arbitrarily cast an object to any type, it would have to be the correct type or a type from which this class inherits. I think Jon is looking to see a fully described scenario, i.e. why are you needing to do this. At the moment we are still unclear and what you are you looking to do doesn't, at least to me, make a whole lot of sense.
Lazarus
@Jon Skeet - For example: I have a list of objects in my app. I would like to take some of these objects and promote them into public properties. Because I like Intellisense and other stuff I would like to make these properties more specific. If my list has objects of type BaseClass I would like have properties SpecialClass and SpecialOtherClass that will expose object of this type that is on the list. So - on the list - general type, properties - more specific. And how to do this?
tomaszs
@Lazarus - updated question to provide complete sceniario where it's usefull for me.
tomaszs
A: 

Now it seems to be impossible, but soon will be available with new feature in .NET 4.0 called "dynamic":

http://www.codeguru.com/vb/vbnet30/article.php/c15645%5F%5F4/

tomaszs