I have a method that I would like to be able to pass a generic list
(such as List<string>, List<int>, etc). I'm trying to figure out what
would be the best type of parameter to use to pass in the list and if
there is a way to find out what type of elements it is designed to
contain (even if it currently has no elements). How would I go about
doing this? 3 9489
Taurin <ta*******@gmai l.comwrote:
I have a method that I would like to be able to pass a generic list
(such as List<string>, List<int>, etc). I'm trying to figure out what
would be the best type of parameter to use to pass in the list and if
there is a way to find out what type of elements it is designed to
contain (even if it currently has no elements). How would I go about
doing this?
public void Foo<T>(List<Tli st)
{
Console.WriteLi ne("T = {0}", typeof(T));
}
In other words, you can find out at execution time with "typeof".
--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Well, if you want to be able to pass a generic list, then I guess you
are talking about a generic method - i.e.
public void SomeMethod<T>(I List<Tlist) {...}
In which case, the answer is typeof(T). If you mean just "a
list" (IList, not IList<T>), then a lot of the standard framework code
is based around inspected the int-indexer. For example, the code below
resolves that the list is a string-list. In the absense of a non-
object int-indexer (i.e. no indexer, or it is just typed as object),
the framework code takes the first (zeroth) element.
There is one other special case worth mentioning; the data-binding
code also respects ITypedList, which tells you about properties of the
list items - this mainly applies to flexible runtime things like
DataTableView.
Marc
using System;
using System.Collecti ons;
using System.Collecti ons.Generic;
using System.Reflecti on;
static class Program
{
static void Main()
{
IList list = new List<string>();
PropertyInfo prop =
list.GetType(). GetProperty("It em", new Type[]
{ typeof(int) });
if (prop != null)
{
Console.WriteLi ne(prop.Propert yType.Name);
}
}
}
Thanks Jon that is EXACTLY what I needed. You're a big help and
quick.
public void Foo<T>(List<Tli st)
{
Console.WriteLi ne("T = {0}", typeof(T));
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: steve bull |
last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class?
For example the code below fails on TSwatch.Exists. How can I get the call to work?
Exists is a method within the Swatch class NOT the SwatchPanel class.
Is this possible?
Suggestions would be very welcome.
|
by: michael.f.ellis |
last post by:
The following script puzzles me. It creates two nested lists that
compare identically. After identical element assignments, the lists
are different. In one case, a single element is replaced. In the
other, an entire column is replaced.
---------------------------------------------------------------------------------------
'''
An oddity in the behavior of lists of lists. Occurs under
Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
|
by: Charles Churchill |
last post by:
I apologize if this question has been asked before, but after about
half an hour of searching I haven't been able to find an answer online.
My code is beloiw, with comments pertaining to my question
In short my question is why when I pass a generic type directly to the
formatObject function it works fine, but when I pass it to the
checkText function where it is itself a generic argument, and then it
is passed to formatObject, it is seen...
|
by: Jakob Bieling |
last post by:
Hi,
I want to move an element from a std::list to the end of the same list.
To get this done, I thought I'd just do something like:
std::list <intlst;
lst.push_back (0);
lst.push_back (1);
lst.push_back (2);
lst.splice (lst.end (), lst, lst.begin (), ++ lst.begin ());
|
by: Jorge Varas |
last post by:
Hi all,
Is it possible to have a collection of a generic type?
for example:
class a<twhere t : class, new
{
}
| |
by: BombDrop |
last post by:
Can any one help I have a method that will return a List to be bound
as a datasource to a combobox see code for population below. I get the
following error when i try to compile
Error 29 Cannot implicitly convert type
'System.Collections.Generic.List<Prsym.ComboPopulation.ComboInfo>' to
'System.Collections.Generic.List<T>
'
|
by: Howard |
last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the
last element in a list? The last() function returns the element itself, not
an iterator.
Thanks,
-Howard
|
by: Jon Slaughter |
last post by:
I have some code like
if (val.GetType().Name == typeof(List<>).Name)
{
}
which lets me determine if val is a generic list(I've already taken care of
the non-generic part but I'd rather be able to compare them like if (val is
List<>) but this obviously doesn't work. (and I can't code it for every
|
by: Andrus |
last post by:
I need to create method which returns my application object type as string :
TypeName(typeof( List<int>)) should return "List<int>" or
"System.Collections.Generic.List<int>"
TypeName(typeof( MyType<Customer>)) should return
"MyNamesp.MyType<MyBusiness.Customer>"
etc.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |