473,799 Members | 3,339 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.get Type())myContro l).OnClientClic k ="code";
}

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

public static john(object obj, LinkButton myControl)
{
myControl.OnCli entClick ="code";
}
public static john(object obj, Button myControl)
{
Button.OnClient Click ="code";
}

thx
Dec 12 '06 #1
4 9484
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(myContro l.GetType().Nam e)
{
case "Button":
case "ImageButto n":
case "LinkButton ":
myControl.getTy pe().GetPropert y("OnClientClic k").SetValue(my Control,"code", null);
return true;
}
return false;
}
"Marc Gravell" <ma**********@g mail.coma écrit dans le message de news:
11************* ********@n67g20 00...legro ups.com...
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.GetT ype().Name)
{
case "LinkButton ":
case "Button":
case "ImageButto n":
IAction ia = ((IAction)((myC ontrol as
Control).Parent .NamingContaine r));
myControl.GetTy pe().GetPropert y("OnClientClic k").SetValue(my Control,
"if(confirm('"+ message+"')){do cument.getEleme ntById('" + ia._ActionID +
"').value=' " + action + "';document.get ElementById('" + 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******@nospa m.nospama écrit dans le message de news:
9E************* *************** **...icrosof t.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).OnC lientClick = "code";
}
else if (c is ImageButton)
{
((ImageButton)c ).OnClientClick = "code";
}
else
{
throw new ArgumentExcepti on("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.OnClientClic k = "code"
}

..... and so forth.

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

public static bool john<T>(T myControl)
{
switch(myContro l.GetType().Nam e)
{
case "Button":
case "ImageButto n":
case "LinkButton ":

myControl.getT ype().GetProper ty("OnClientCli ck").SetValue(m yControl,"code" ,null);
return true;
}
return false;
}
"Marc Gravell" <ma**********@g mail.coma écrit dans le message de news:
11************* ********@n67g20 00...legro ups.com...
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
5188
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 a nebulous statement, but I am looking for a generic way to convert a variable of unknown type to its actual type. Perhaps a better example would be
3
4465
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 member members. i.e. I do not want to use GetType(<GenericType>(Of String).IsAssignableFrom(<MyType>)
1
4398
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 'System.Collections.Generic.IComparer `1' My code looks like the following: List<AccountDB.Queue> oList = getAllQueuesFunction(); oList.Sort(New GenericComparer("QueueName"));
2
9868
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
5930
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 problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
5
9634
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 class to produce values in the system. For instance I could create class MsgGenericValue<UInt16>() which would represent an unsigned value on the interface.
9
8851
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 an answer. Even pacing around my flat until I nearly went into an altered state of reality proved futile. So here is the problem... When I link up the ObjectDataSource UpdateMethod to a method in the
2
1576
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
4187
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 types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10484
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.