Linked Questions
263 questions linked to/from How do I use reflection to call a generic method?
20
votes
2answers
1k views
How to conditionally invoke a generic method with constraints? [duplicate]
Suppose I have an unconstrained generic method that works on all types supporting equality. It performs pairwise equality checks and so works in O(n2):
public static int CountDuplicates<T>(...
5
votes
2answers
16k views
How to get class of type [duplicate]
I need to use method like:
DoSomething<(T)>();
But i don't know which Type i have, only object of class Type. How can i call this method if I have only:
Type typeOfGeneric;
5
votes
2answers
8k views
Use variable as Type [duplicate]
Is it possible to make such code work?:
private List<Type> Models = new List<Type>()
{
typeof(LineModel), typeof(LineDirectionModel), typeof(BusStopTimeModel), typeof(...
3
votes
1answer
3k views
Call generic extension method with a dynamic type [duplicate]
I'm trying to execute an extension method that returns and object of type T, but I'm trying to have type T dynamic based on a Header/Detail dynamic generic type.
This maybe a bit verbose...
using ...
3
votes
2answers
2k views
How to pass a System.Type into a generic method using reflection [duplicate]
Possible Duplicate:
How to use reflection to call generic Method?
I'm trying to simplify some EF Code First configuration.
Instead of writing code like this:
protected override void ...
4
votes
2answers
709 views
Generic method execute with a runtime type [duplicate]
It is strange but the source code
public class Processor<T> where T: class
{
...
private object WorkWithSubtype(IRequester nextRequester, Type type)
{
if (type.GetInterface("...
0
votes
1answer
698 views
C# using generic method with Type [duplicate]
I'm using .Net framework 2.0 to try and do the following:
I've an external service which returns a list of int. In turn I use each int to find a corresponding Type which has an Attribute with a ...
2
votes
2answers
101 views
Call a generic method and set the generic type at runtime [duplicate]
In the example bellow, is it possible to set the Classname type parameter dynamically?
UpdateAndSave<Classname>>().Execute(sql)
-1
votes
4answers
340 views
ClassName<Type.GetType(“Somenamespace.ClassName”)> [duplicate]
Basically what I need is to use the Type that i got using Type.GetType in a Generic Type,
is it possible, if yes how ?
I need something like this:
Type t = Type.GetType("mynamespce.a.b.c");
var x = ...
5
votes
1answer
1k views
Type.GetMethod with a Generic overload - Get Generic MethodInfo without looping through all Methods [duplicate]
I Have two functions:
public void DeleteRecord(int id);
public T DeleteRecord<T>(int id);
Here's how i try to dynamically invoke the generic method:
MethodInfo method = typeof(DAL).GetMethod("...
0
votes
2answers
979 views
Call Generic Method with Dynamic Type (.net 3.5) [duplicate]
I am trying to call a method on a class to sort data of a datatype that is unknown until the method is called.
Here is the code for the sort procedure that tries to call the generic method:
public ...
3
votes
1answer
483 views
Pass a generic type 'class' when only having a string [duplicate]
In C# I have a method
public string GetString<T>() where T : class
In the calling function I have only the name of the class which I want to pass to GetString()<T>. E.g. "MyClass". How ...
0
votes
3answers
243 views
Reading type <T> from config [duplicate]
Possible Duplicate:
How to use reflection to call generic Method?
I have a method with the below signature
public string Register<T>()
{
//code
}
which can be invoked like this
var ...
1
vote
1answer
332 views
Get type of property in generic method [duplicate]
I have method:
static T RandomObject<T> (...) {
var tmp = Activator.CreateInstance<T>();
...
foreach (PropertyInfo info in tmp.GetType().GetProperties()){
...
}
}
And if class T ...
0
votes
1answer
524 views
Convert string to class name and pass as parameter T [duplicate]
I'm looking to convert the string name of a class to a class, then pass that class name to a method which accepts <T>:
var objectList = _reader.GetObjects<MyClassName>();
and the method ...