tags:

views:

66

answers:

3

I'm currently trying to get the type that an object was casted as in a later part of my application. This is primarily for a crazy situation that came up that would be a whole lot cleaner if I can get this to work. I wrote the following unit test that hopefully explains what I'm hoping to get working.

using System;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace Test.Helper
{
    [TestFixture]
    public class CastingTests
    {
     public interface IStub { }
     public class Stub : IStub { }

     protected static Type GetCast(object sample)
     {
      //TODO work on getting method to return casted type
      throw new NotImplementedException();
     }

     [Test]
     public void GettingCastReturnsCastedType()
     {
      IStub stub = new Stub();
      Type type = GetCast(stub);
      Assert.That(type, Is.EqualTo(typeof(IStub)));
     }
    }
}

Pretty much I need to find a way to get the IStub type from the sample object dynamically. In the application it could be whatever the object was casted as when it was passed in. I have no idea if this is even possible, but would be pretty cool if it is. Thanks for your help in advance!

+1  A: 

I can't be 100% sure, but I believe you would be able to do something along these lines:

protected static Type GetCast<T>(T obj)
{
   return typeof(T); // NOT obj.GetType();
}

and then:

public void GettingCastReturnsCastedType()
{
    IStub stub = new Stub(); // casted as an IStub
    Type type = GetCast(stub); // see what this returns
}
LorenVS
I would use generics, but the location where I want to actually do this has a method parameter of (params object[] iocRegistrations). This wouldn't really allow me to use generics from what I understand.
basicdays
+3  A: 

Casting an object does not have any actual effects on that object, with the possible exception of if that class were to define an explicit conversion that had performed an operation on the original value that produced a side effect...a truly obscure and unintuitive situation.

As for a general solution, no, there is no way to determine what it was "before", because it is still the same object, just viewed from the code's perspective as a different type. The instance itself is the same.

Adam Robinson
Makes sense, I'll probably just have to pass both the object and the Type to the methods that I'll be using then.
basicdays
+1  A: 

The type of an object does not change by casting, so there is no way of seeing if an object has been casted to this or that type. It is just the type of an expression, variable or field that represents the target type of the cast.

So the best you can do is analyzing the method parameters or fields to get the type - but this should be of little use, because it is static information availiable at compile time.

And what I wonder all the time ... what the heck are you trying to achieve?

Daniel Brückner
Ha, yeah, that's a good question. I'm currently using both CSLA and Castle Windsor. My unit tests were getting obnoxious with the support methods I had to write just to be able to set mocks and stubs into a Service Locator. I was hoping to write a test helper that could accept a list of objects and register them by the cast type. I may just end up creating a Registration class that has both a Type and the object to register to loop through that.
basicdays