Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Pass Generics At Runtime

I have a method like the following:

public IEnumerable<T> GetControls<T>()
 : where T : ControlBase
{
 // removed.
}

I then created a class:

public class HandleBase<TOwner> : ControlBase
 : TOwner
{
 // Removed
}

I'd like to be able to call

GetControls<HandleBase<this.GetType()>>; 

where it would use the type of THIS class to pass to the HandleBase. This would in essentially get all HandleBase that have an owner of THIS type.

How can I achieve this?

EDIT:

I'm using .NET 2.0 so solutions greater than 2.0 will not work.

The idea is to have ControlBase have a collection of other ControlBase for "children". Then they can be queried based on their type with GetControls<T>(). This would allow me to, for example, get all HandleBase for a Shape. Then I can take all of these and set Visible=false or do something else with them. Thus I can manipulate children of a specific type for a collection.

HandleBase<TOwner> requires the TOwner since it has a reference to the "owning type". So you can only add anything that extends HandleBase to a Shape. Make sense?

Thanks for all the help!

like image 211
TheCloudlessSky Avatar asked Apr 09 '10 14:04

TheCloudlessSky


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.

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.

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.


1 Answers

You can do this either by specifying a type at compile-time or by using reflection.

You can do it with reflection like this:

typeof(SomeClass).GetMethod("GetControls")
    .MakeGenericMethod(typeof(HandleBase<>).MakeGenericType(GetType()))
    .Invoke(someObject, null);

Note that it would return an object; you would not be able to cast it to IEnumerable<T> (Unless you know what T is at compile-time, in which case there's no point). You would be able to cast it to IEnumerable.

However, this is a bad idea.
There is probably a better solution for you; please provide more detail.

like image 178
SLaks Avatar answered Sep 30 '22 02:09

SLaks