开发者

.net Generic Calls <T>

开发者 https://www.devze.com 2022-12-23 16:51 出处:网络
I have a function that accepts a generic parameter T that is of type class like so : publicFunc<T, bool> MyMethod<T>(string paramName, object value) where T : class

I have a function that accepts a generic parameter T that is of type class like so :

public  Func<T, bool> MyMethod<T>(string paramName, object value) where T : class

开发者_如何学GoBut when calling the function I do not have direct access to the class that needs to be the parameter.

MyMethod<foo>("foo1", "foo2")

Is there a way I can get the class foo via other means like reflection so I can use the function?

UPDATE:

This method in particular performs a Lambda expression through LINQ and checks if the parameter value is equal to the value I passed... So T value will not make sense i.e. that's why I need an object type. Also My real problem is that I can't do something like;

MyMethod<fooInstance>("foo1","foo2")

and I need to get a reference of the Type rather than class. This because the class name is encrypted so i need to get the Type reflection using the Activator class and assign the object returned to the generic call of MyMethod function

example:

object o = Activator.CreateInstance(Type.GetType(Assembly.GetExecutingAssembly().GetName().Name + ".Foo"));
    MyMethod<o.getType()>("foo1","foo2");


You can use

Type type = typeof(T);

to get the type passed in as T.


If I understand you question correctly I don't think you'll get much gain in using a generic method here.

It would be useful if your method signature where:

public Func<T, bool> MyMethod<T>(string paramName, T value) where T : class
0

精彩评论

暂无评论...
验证码 换一张
取 消