473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

generic - getType for cast - c# .Net2 aspx

hello,
i want to find the type from a generic... like :
public static john<T>(object obj, T myControl)
{
((myControl.getType())myControl).OnClientClick ="code";
}

because my generic var can be a Button, LinkButton, ... with "OnClientClick
" event.
and i dont want use :

public static john(object obj, LinkButton myControl)
{
myControl.OnClientClick ="code";
}
public static john(object obj, Button myControl)
{
Button.OnClientClick ="code";
}

thx
Dec 12 '06 #1
4 9466
Normally, that wouldn't be a generics question, but rather a base-class
/ interface question.

However, in this case the OnClientClick property isn't exposed in a
base-class / interface; one solution here would be reflection;
*however* since there are only 3 classes (Button, ImageButton and
LinkButton) with this property, I would actually recommend the
overloaded form that you have written. It will be a lot faster.

Marc

Dec 12 '06 #2
i code that :

public static bool john<T>(T myControl)
{
switch(myControl.GetType().Name)
{
case "Button":
case "ImageButton":
case "LinkButton":
myControl.getType().GetProperty("OnClientClick").S etValue(myControl,"code",null);
return true;
}
return false;
}
"Marc Gravell" <ma**********@gmail.coma écrit dans le message de news:
11*********************@n67g2000cwd.googlegroups.c om...
Normally, that wouldn't be a generics question, but rather a base-class
/ interface question.

However, in this case the OnClientClick property isn't exposed in a
base-class / interface; one solution here would be reflection;
*however* since there are only 3 classes (Button, ImageButton and
LinkButton) with this property, I would actually recommend the
overloaded form that you have written. It will be a lot faster.

Marc

Dec 12 '06 #3
As Dale and myself already observed : you aren't using the generic, so
don't code it as though you are. Generics cannot solve this problem.
Reflection will work, but will be the slowest of the three options.
Overloading will be the fastest, but requires the object to be typed
when calling. If all you know is that it is a control (or object,
even), then Dale's type-testing is your best bet.

Marc

Dec 12 '06 #4
Hi,
"Why not just do this:" : because my "code" section is "hard" and i dont
want repeat it three time :
public static bool createLink<T>(T myControl, string message, string action,
string code)
{
switch (myControl.GetType().Name)
{
case "LinkButton":
case "Button":
case "ImageButton":
IAction ia = ((IAction)((myControl as
Control).Parent.NamingContainer));
myControl.GetType().GetProperty("OnClientClick").S etValue(myControl,
"if(confirm('"+message+"')){document.getElementByI d('" + ia._ActionID +
"').value='" + action + "';document.getElementById('" + ia._ValueID +
"').value='" + code + "'; }else return false;", null);
return true;
}
return false;
}

generic, reflection, ..., no importance... i want just no repeat my code...
and i wnt take take the best/good way to do it.

"Dale" <da******@nospam.nospama écrit dans le message de news:
9E**********************************@microsoft.com...
Why use a generic at all? Why not just do this:

public static john(object obj, control c)
{
if (c is LinkButton)
{
((LinkButton)c).OnClientClick = "code";
}
else if (c is Button)
{
((Button)c).OnClientClick = "code";
}
else if (c is ImageButton)
{
((ImageButton)c).OnClientClick = "code";
}
else
{
throw new ArgumentException("Only Button, ImageButton, or
LinkButton objects can be passed to the Control parameter of John(object
obj,
Control c).");
}
}

Alternatively, a more fxCop friendly way of doing the if would be:

LinkButton lb;
if ((lb = c as LinkButton) != null)
{
lb.OnClientClick = "code"
}

..... and so forth.

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Steph" wrote:
>i code that :

public static bool john<T>(T myControl)
{
switch(myControl.GetType().Name)
{
case "Button":
case "ImageButton":
case "LinkButton":

myControl.getType().GetProperty("OnClientClick"). SetValue(myControl,"code",null);
return true;
}
return false;
}
"Marc Gravell" <ma**********@gmail.coma écrit dans le message de news:
11*********************@n67g2000cwd.googlegroups.c om...
Normally, that wouldn't be a generics question, but rather a base-class
/ interface question.

However, in this case the OnClientClick property isn't exposed in a
base-class / interface; one solution here would be reflection;
*however* since there are only 3 classes (Button, ImageButton and
LinkButton) with this property, I would actually recommend the
overloaded form that you have written. It will be a lot faster.

Marc



Dec 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
3
by: Joe Adams | last post by:
Hi All, How can I use GetType(<GenericType>).IsAssignableFrom(<MyType>) I need to now if the <MyType> is the same type of class as the <GenericType> without having to add the generic type...
1
by: INeedADip | last post by:
I am trying to use the following generic (reflection) class as the ICamparer parameter for a generic list..but I get the error: "Unable to cast object of type 'GenericComparer' to type...
2
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
5
by: vtjumper | last post by:
I'm building a C# interface to an existing messaging system. The messaging system allows values of several types to be sent/recieved over the interface. What I want to do is use a generic...
9
by: Kernel Bling | last post by:
Hi Everyone, This Saturday the stage was set. The problem simply could not go on existing -- it had to be solved. Many hours, articles, compilations and frustrations later I still did not find...
2
by: ADN | last post by:
Hi, I have a method which calls my service factory: class person { public int ID { get; set: } public string Name {get; set;} } IList<Personmypeople = __serviceFactory.Fetch(new Person())
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.