-4

I would like to use a generic type for casting, is there any simple way to do this? Here is an example:

var resource = viewBase.getAnyInstance(resourceName) as T;

Where T is one of existing type.

Any suggestions would be appreciated.

Greg
  • 11,302
  • 2
  • 48
  • 79
AboutNothing
  • 97
  • 3
  • 9

1 Answers1

1

A Generic is a fancy way of handling multiple types of Objects without any clear concise knowledge of what your manipulating. That in itself creates quite a bit of flexibility, as it can alleviate several issues you may encounter when you utilize Boxing and Unboxing.

To help clarify Generics for you, I'll use the List example. Mostly because at least for me it really solidified Casting with Generics.

The problem, if you've tried to Cast between List Generics you'll quickly see... You can't.

In my example I'll use Shapes, so in theory you have three classes.

Classes:

  • Circle
  • Square
  • Polygon

When you learned Casting this is quite easy to do.

int i = 10;
string j = "I have ";
string d = j + (string)i;

And you've converted your variable i into a String. As long as the type fits; your okay. If it doesn't you'll encounter exceptions.

When you introduce Generics it changes quickly.

Now, as you saw above we have three Classes that represent Shapes. Now a Shape will be a common Object which technically should be inherited by those Shapes. So List<Square> can't inherit from List<Circle> and vice-versa. It seems quantitative, seems plausible. But it isn't, why?

It's due to the Constructed Type in which the conversion of data takes place.

So in some instances for these List you'd have to write a Loop to transfer data from one List to another.

So this is where Generics can be quite useful.

// Square List:
List<Square> lSq = new List<Square>();

   lSq.Add(new Square("Red Square"));
   lSq.Add(new Square("Blue Square"));

We've created a Square List to hold our object.

// Generic Shape
List<Shape> shp = lSq.ConvertAll(
   new Converter<Square, Shape>(lSqToshp));

We've actually used a Generic to Cast those values directly into our new List.

Now, I understand that isn't your question. Your asking:

I would like to use a generic type for casting, is there any simple way to do this?

The short answer, yes. But the variation will vary on your goal and usage. You've mentioned that it is an Abstract Class.

Now, are you trying to do something like:

public void SomeMethod(object caller)
{
   someClass<T> info = caller as someClass<T>;
   if (info != null)
   {}
}

That would describe Casting to an Abstract Class.

var detail = (SomeClassBase)viewBase.getAnyInstance(detailName); 

There are a lot of variations.

A post that sounds similar to your vague question here. Another post that may provide more details here. Something to note, if your trying to pass an Object that is already known, why not just use the caller directly?

Unfortunately, your question will require more details for a better answer. But it sounds like your having difficulties understanding Generics.

Community
  • 1
  • 1
Greg
  • 11,302
  • 2
  • 48
  • 79
  • Thank you so much for answer and comments. I am currently looking into it and trying to understand the problematic in more complex way. Thanks again! – AboutNothing Feb 09 '13 at 10:22
  • No problem. Hopefully my response helped a bit. It helped me understand but others it may not. – Greg Feb 10 '13 at 08:13