473,385 Members | 1,324 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,385 software developers and data experts.

Generic Type

Airslash
221 100+
Hello,

Hope I can explain this properly...

I'm building sort of SettingsManager class that keeps track of various settings off the application. I'm aware there are build in stuff for that in C#, but thats not the issue here.

In the XML, I make it simply by constructing the file like this:

<?xml version="1.0" ?>
<setting type="" name="">value</setting>
<setting type="" name="">value</setting>
</xml>

I've designed a class that looks like this:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Tools.Config
  7. {
  8.     public class Setting<T>
  9.     {
  10.         #region Constructors
  11.  
  12.         /// <summary>
  13.         /// Default constructor of the Setting class. Creates a new instance
  14.         /// of the Setting class and initializes the internal fields to the values
  15.         /// provided. The value of the class will be set to the default value of
  16.         /// the specified type.
  17.         /// </summary>
  18.         /// <param name="name">The name of the Setting.</param>
  19.         public Setting(String name)
  20.             : this(name, default(T))
  21.         { }
  22.  
  23.         /// <summary>
  24.         /// Overloaded constructor of the Setting class. Creates a new instance
  25.         /// of the Setting class and initializes the internal fields to the
  26.         /// values provided.
  27.         /// </summary>
  28.         /// <param name="name">The name of the Setting.</param>
  29.         /// <param name="value">The value of the Setting.</param>
  30.         public Setting(String name, T value)
  31.         {
  32.             Name = name;
  33.             Value = value;
  34.         }
  35.  
  36.         #endregion
  37.  
  38.         #region Public Properties
  39.  
  40.         /// <summary>
  41.         /// Gets or sets the name of the Setting.
  42.         /// </summary>
  43.         public String Name
  44.         {
  45.             get { return m_name; }
  46.             set { m_name = value; }
  47.         }
  48.  
  49.         /// <summary>
  50.         /// Gets or sets the value of the Setting.
  51.         /// </summary>
  52.         public T Value
  53.         {
  54.             get { return m_value; }
  55.             set { m_value = value; }
  56.         }
  57.  
  58.         #endregion
  59.  
  60.         #region Private Fields
  61.  
  62.         private T m_value;      // The value of the setting.
  63.         private String m_name;  // The name of the setting.
  64.  
  65.         #endregion
  66.     }
  67. }
  68.  
It's pretty obvious what that class does.
Now I'm looking for a way that if I read the value of the Type attrbute in a XML document, how I can use that string to do this:

Setting<Type> x = new Setting<Type>(name, value);

where Type is the string value from the type attribute in the XML document.
Jul 23 '10 #1
4 1726
GaryTexmo
1,501 Expert 1GB
I feel like we had a thread about this a while back but I can't remember what the outcome was... I'll try to take a look. I played around and found this in another thread here, posted long ago. I don't know if it'll help or not, but it does let you create a generic list from a type.

Expand|Select|Wrap|Line Numbers
  1.             Type theType = Type.GetType("System.Collections.Generic.List`1[System.String]");
  2.             object o = Activator.CreateInstance(theType);
  3.             Console.WriteLine(o.GetType());
Jul 23 '10 #2
Airslash
221 100+
exactly what I was looking for :).
Haven't played around much with reflection yet, but I know it can do magic
Jul 23 '10 #3
GaryTexmo
1,501 Expert 1GB
Haha glad it helped!

I was mucking about with this and discovered a bit more...

Expand|Select|Wrap|Line Numbers
  1.             Type theType = Type.GetType("System.Collections.Generic.List`1[System.String]");
  2.             object o = Activator.CreateInstance(theType);
  3.             Console.WriteLine(o.GetType());
  4.  
  5.             MethodInfo addMethod = theType.GetMethod("Add");
  6.             addMethod.Invoke(o, new object[] { "Item 1" });
  7.             addMethod.Invoke(o, new object[] { "Item 2" });
  8.  
  9.             PropertyInfo countProperty = theType.GetProperty("Count");
  10.             int count = (int)countProperty.GetValue(o, null);
  11.             Console.WriteLine(count);
Obviously I'm not null checking... tsk tsk, bad!

I still can't figure out how to loop through the darn data yet...
Jul 23 '10 #4
GaryTexmo
1,501 Expert 1GB
There we go...
Expand|Select|Wrap|Line Numbers
  1.             List<string> test = new List<string>();
  2.  
  3.             Type theType = Type.GetType("System.Collections.Generic.List`1[System.String]");
  4.             object o = Activator.CreateInstance(theType);
  5.             Console.WriteLine(o.GetType());
  6.  
  7.             MethodInfo addMethod = theType.GetMethod("Add");
  8.             addMethod.Invoke(o, new object[] { "Item 1" });
  9.             addMethod.Invoke(o, new object[] { "Item 2" });
  10.  
  11.             PropertyInfo countProperty = theType.GetProperty("Count");
  12.             int count = (int)countProperty.GetValue(o, null);
  13.             Console.WriteLine(count);
  14.  
  15.             PropertyInfo itemProperty = theType.GetProperty("Item");
  16.             for (int i = 0; i < count; i++)
  17.             {
  18.                 Console.WriteLine(itemProperty.GetValue(o, new object[] { i }));
  19.             }
Jul 23 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Cool Guy | last post by:
I don't understand the third paragraph under the heading 'Generic type instantiations' on <http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>: | The .NET Common Language Runtime...
8
by: Cool Guy | last post by:
From <http://msdn2.microsoft.com/en-us/library/f4a6ta2h(en-US,VS.80).aspx>: | As with the previous use of the Stack<T> class created with the Order | type, another instance of the specialized...
1
by: Tommaso Caldarola | last post by:
delegate void SomeDelegate<T>() where T : BaseClass; my_event(object sender, myEventArgs e) { //e.Tag object is SomeDelegate<DerivedClass> SomeDelegate<BaseClass> del =...
4
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...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
1
by: interX | last post by:
Hi I'm new in VC++ and have a question to generics. I have a generic class, which contains an array of the generic type. This array I can pin and then I would like to get an unmanaged pointer to...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
3
by: webcliff | last post by:
Please help me to look at the following code: -------------start---------- using System; using System.Collections.Generic; public class GenType { }
9
by: tadmill | last post by:
Is it possible to pass a generic parameter of the same class to to its constructor, where the "T" type passed in the constructor is different than the "T" type of the instanced class? ie, ...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.