how to specify the T Generic type for a class at runtime or from parameter [modified]
-
You can't. Generics are (IIRC) really just syntaxic sugar that tells the compiler to write code for each Type you specify in the Generic. Say you pass two different types of objects to a Generic method. The compiler automatically writes two methods, using your code as a template, to handle both cases of those objects passed to it. Types used by Generics are determined at compile-time, not run-time.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Generic reference types are more than syntactic sugar; the compiler only has to produce one JIT-compiled instance of a method that uses a generic reference type, but the method can produce objects which are 'ready to go' in the desired type without the expense of a typecast or other run-time validation. I know of no way such a result could be achieved without generics. There are limits to what generics can do, but they seem to be a very useful feature with significant semantic meaning. Extension methods, by contrast, seem more like syntactic sugar.
-
You can't. Generics are (IIRC) really just syntaxic sugar that tells the compiler to write code for each Type you specify in the Generic. Say you pass two different types of objects to a Generic method. The compiler automatically writes two methods, using your code as a template, to handle both cases of those objects passed to it. Types used by Generics are determined at compile-time, not run-time.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008thanks a lot for your replay and explanation but could you tell me how can I use the second choice I mean how to make the user control accept T as Template something like this :
public class kokoControl : UserControl
{
.....
}You have To Search About The Truth Of Your Life Why Are you Here In Life ?
-
thanks a lot for your replay and explanation but could you tell me how can I use the second choice I mean how to make the user control accept T as Template something like this :
public class kokoControl : UserControl
{
.....
}You have To Search About The Truth Of Your Life Why Are you Here In Life ?
What are you ultimately trying to do with this??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
What are you ultimately trying to do with this??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008ok I'm sorry my replay was not complete . i will tell you the full story :) I made generic class that can make DynamicLinq For any class this class is
public class LinqFilter<T>
{
some of :
public Fun<T,bool> GetFilter()
{
//here i will return
Expression Filter ;
Filter = Expression.And(.....)
..............................
var predicate = Expression.Lambda. .... .
return (Func<T,bool> )predicate.Compile();
}
}the previous class work well I want to use it inside a user control called FilterControl but I want to make the control also as a Generic Class just to pass the T inside the control
public Class FilterControl<T> : UserControl
{
LinqFilter<T> F;
................
}but I got an error when tried to do that how can I pass the T from outside the control to the [F] Variable of linqfilter .:confused: thanks
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
-
ok I'm sorry my replay was not complete . i will tell you the full story :) I made generic class that can make DynamicLinq For any class this class is
public class LinqFilter<T>
{
some of :
public Fun<T,bool> GetFilter()
{
//here i will return
Expression Filter ;
Filter = Expression.And(.....)
..............................
var predicate = Expression.Lambda. .... .
return (Func<T,bool> )predicate.Compile();
}
}the previous class work well I want to use it inside a user control called FilterControl but I want to make the control also as a Generic Class just to pass the T inside the control
public Class FilterControl<T> : UserControl
{
LinqFilter<T> F;
................
}but I got an error when tried to do that how can I pass the T from outside the control to the [F] Variable of linqfilter .:confused: thanks
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
So I fired up Snippet Compiler[^], added System.Core.dll as a reference, and wrote this little snippet of code:
public class Base
{
public Func<t,> GetFilter()
{
return delegate(T t) { return true; };
}
}public class BaseCotnrol : UserControl
{
Base foo;
}It compiled just fine. Could you post the compiler error?
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
So I fired up Snippet Compiler[^], added System.Core.dll as a reference, and wrote this little snippet of code:
public class Base
{
public Func<t,> GetFilter()
{
return delegate(T t) { return true; };
}
}public class BaseCotnrol : UserControl
{
Base foo;
}It compiled just fine. Could you post the compiler error?
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
first , thanks a lot for your replay :) I thought that I will not get the answer :^) !! I will try that but could you explain more what should this code do ?
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
-
first , thanks a lot for your replay :) I thought that I will not get the answer :^) !! I will try that but could you explain more what should this code do ?
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
My code doesn't really do anything. &l;smile /> I just wrote some code that looked like your code to make sure that it compiles. It compiled. Unfortunately, that just means that I don't understand the nature of your question. Can you post something more indicative of your plight?
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
My code doesn't really do anything. &l;smile /> I just wrote some code that looked like your code to make sure that it compiles. It compiled. Unfortunately, that just means that I don't understand the nature of your question. Can you post something more indicative of your plight?
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
I will explain I made this class
public class LinqFilter<T>
{
public Fun<T,bool> GetFilter()
{
// in this function I made an Expression Tree after
// that i make lambda expression that return delegate
// this is not important for my problem
// but I said that just to not write empty function .
var predicate = Expression.Lambda. .... .
return (Func<T,bool> )predicate.Compile();
}
}there is a user control called FilterControl I need to use object of previous class [LinqFilter] inside this control but I want to get The [T] Type from out side the user control the following code make compiler error when i put <T> in User Control definition
public Class FilterControl<T> : UserControl
{
LinqFilter<T> F;
................
}I hope to not give me joking code again thanks
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
-
I will explain I made this class
public class LinqFilter<T>
{
public Fun<T,bool> GetFilter()
{
// in this function I made an Expression Tree after
// that i make lambda expression that return delegate
// this is not important for my problem
// but I said that just to not write empty function .
var predicate = Expression.Lambda. .... .
return (Func<T,bool> )predicate.Compile();
}
}there is a user control called FilterControl I need to use object of previous class [LinqFilter] inside this control but I want to get The [T] Type from out side the user control the following code make compiler error when i put <T> in User Control definition
public Class FilterControl<T> : UserControl
{
LinqFilter<T> F;
................
}I hope to not give me joking code again thanks
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
When you write that you "want to get the [T] Type from out side the user control", what do you mean by that? Do you mean that you want to bind
T
at runtime rather than at compile time? Something like the following pseudocode?t <- assign some resolved Type
create an instance of FilterControl dynamically and bind t to T
put the instance of FilterControl`1 on your form"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
When you write that you "want to get the [T] Type from out side the user control", what do you mean by that? Do you mean that you want to bind
T
at runtime rather than at compile time? Something like the following pseudocode?t <- assign some resolved Type
create an instance of FilterControl dynamically and bind t to T
put the instance of FilterControl`1 on your form"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
What I need is : When I put the FilterControl in the window , I want to specify the T Type I mean in Compile Time in this way the control will be able to make filtering on objects of any T class with support from LinqFilter class
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
-
What I need is : When I put the FilterControl in the window , I want to specify the T Type I mean in Compile Time in this way the control will be able to make filtering on objects of any T class with support from LinqFilter class
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
Ah ha! Now I understand. The Visual Studio designers do not support controls with unbound generic parameters. You can't create your
FilterControl<T> : UserControl
and then use the forms designer to drag it onto a panel or form. You might be able to adapt aTypeDescriptor
for the user control for your forms, though I've never seen this done. There's an article on Urban Potato that does this for controls withabstract
base classes: Using Visual Studio Whidbey to Design Abstract Forms[^]. -
Ah ha! Now I understand. The Visual Studio designers do not support controls with unbound generic parameters. You can't create your
FilterControl<T> : UserControl
and then use the forms designer to drag it onto a panel or form. You might be able to adapt aTypeDescriptor
for the user control for your forms, though I've never seen this done. There's an article on Urban Potato that does this for controls withabstract
base classes: Using Visual Studio Whidbey to Design Abstract Forms[^].thanks a lot I will read this article ( in sha'a allah) and I hope to find what I want Thanks very match :)
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
-
thanks a lot I will read this article ( in sha'a allah) and I hope to find what I want Thanks very match :)
You have To Search About The Truth Of Your Life Why Are you Here In Life ?
If you find or develop something interesting, then don't forget to come back and post a solution so that we all can learn! :-D
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
-
If you find or develop something interesting, then don't forget to come back and post a solution so that we all can learn! :-D
"we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty
Okay my friend :)
You have To Search About The Truth Of Your Life Why Are you Here In Life ?