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

Mixed type Dictionary

Is there any way to have a mixed type Dictionary object? For example
something like:
dictionary.Add(3, "foobar");
dictionary.Add("foo", 33444);

I am trying to make a generic parameter list without reverting to
Dictionary<object, object>. Any ideas on where to look for this sort of
thing? I have been doing a lot of work with generics lately, but have
not found anything to fit the bill.

Thanks.

Oct 15 '06 #1
1 2889
Well, it is hard to do without boxing... but can be done...

Look at the following; you can (as examples);

ParameterSet ps = new ParameterSet();
ps.Add("Test", 5);
ps.Add<int>("SomeParam");
int val = ps.GetValue<int>("Test");
ps.SetValue("SomeParam", 17);
IParameter param = ps["Test"];
param.Type...
param.Value...
param.Name...

etc

Marc

===

public interface IParameter
{
string Name { get;}
object Value { get; set;}
Type Type { get; }
}
public interface IParameter<T: IParameter
{
new T Value { get; set;}
}
public class Parameter<T: IParameter, IParameter<T>
{
public Type Type { get { return typeof(T); } }
readonly string name;
public string Name { get { return name; } }
T value;
public T Value {
get { return this.value; }
set { this.value = value; }
}
object IParameter.Value
{
get { return Value; }
set { Value = (T) value; }
}
public Parameter(string name) : this(name, default(T)) {}
public Parameter(string name, T value) {
this.name = name;
this.value = value;
}
}
public class ParameterSet
{
readonly Dictionary<string, IParameterparameters = new
Dictionary<string, IParameter>();
IParameter this[string name]
{
get { return parameters[name]; }
}
void Add(IParameter parameter)
{
parameters.Add(parameter.Name, parameter);
}
void Add<T>(string name) {
Add(name, default(T));
}
void Add<T>(string name, T value)
{
Add(new Parameter<T>(name, value));
}
T GetValue<T>(string name)
{
return ((IParameter<T>)parameters[name]).Value;
}
void SetValue<T>(string name, T value)
{
((IParameter<T>)parameters[name]).Value = value;
}
}

Oct 15 '06 #2

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

Similar topics

0
by: Swaroop Kumar | last post by:
Hi: I'm trying to write a schema that contains information as described below: 1. The first element is a mandatory fixed string. 2. The second element is a mixed content element that can...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
2
by: bearophileHUGS | last post by:
Notes: - This email is about Mark Dufour's Shed Skin (SS) (http://shed-skin.blogspot.com), but the errors/ingenuousness it contains are mine. My experience with C++ is limited still. - The...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
7
by: nugget | last post by:
Role-based security for an ASP/ASP.NET mixed environment Hello: My co-worker and I have been charged with designing role-based security for our intranet. The technologies we have to work with...
4
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
7
by: =?Utf-8?B?SmltIFdhbHNo?= | last post by:
I'm new to working with mixed assemblies. All of my previous experience has been with VC++/MFC in native, unmanaged applications. When I create a mixed assembly in which one or more of the files...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.