I have my constants in a class that looks like this:
public partial class CODE
{
public struct Status
{
public const long Registered = 5;
public const long Active = 6;
}
}
I would like to use lambda on the constants (this won't work):
var foo = CODE.Status.Where(x=> x > 5);
I have made a method that generates a dictionary from my structs which is somewhat close to what I need. The problem is that I don't get any intellisense on the sections. I have to pass them as strings (due to the dynamic datatype).
Is there any way to get my sample above working with my current constant class?
private Dictionary<string, Dictionary<string, dynamic>> GenerateConstants(
List<Type> classTypes)
{
var ret = new Dictionary<string, Dictionary<string, dynamic>>();
foreach (Type t in classTypes)
{
var fields = new Dictionary<string, dynamic>();
var structs = t.GetNestedTypes().Where(x => x.IsValueType &&
!x.IsPrimitive && !x.IsEnum);
foreach (var nestedStruct in structs)
{
var innerFields = new Dictionary<string, dynamic>();
foreach (var field in nestedStruct.GetFields())
{
innerFields.Add(field.Name, field.GetValue(null));
}
fields.Add(nestedStruct.Name, innerFields);
}
foreach (var field in t.GetFields())
{
if (field.FieldType == typeof(long))
{
fields.Add(field.Name, field.GetValue(null));
}
}
ret.Add(t.Name, fields);
}
return ret;
}
var constants = GenerateConstants(new List<Type> { typeof(CODE) });
var foo = constants.FirstOrDefault(x => x.Key == "CODE");
CODE.Status would never work, as is it presented in question, cause Status is a type inside your class and, not it's member.
You can think about something like:
public partial class CODE
{
public struct Status
{
public static readonly long Registered = 5; //STATIC
public static readonly long Active = 6; //STATIC
}
}
In this way you can access those fields, but it's not clear to me if this completely answers your question.
If it's not, please clarify.
If you want a list of items that you where you can adress the keys with intellisense I would make something along the following lines:
public partial class CODE
{
public const long Registered = 5;
public const long Active = 6;
public List<long> StatusList;
public Status()
{
StatusList = new Dictionairy<int, long>();
StatusList.Add(Registered);
StatusList.Add(Active);
}
}
Since it's a List you should be able to use Linq and you can use the public const values to check your 'item type' (e.g. if you do a foreach on your list you could use them in if/else statements or a switch inside your loop).
Actual implementation could be fancier (Dictionary with Enumerable as key and long as value), but that would depend on what you want to do with it.
Related
I'm trying to implement a class with a property which can be accessed only with parameter. To clear my question see how I intend to use it
Note that this is different than Indexer. Please don't flag for duplicate.
My incomplete class
public class Inventory{
public object Options..... // I don't know how to define this property
}
How I'm going to use it
Inventory inv = new Inventory();
string invLabel = (string)inv.Options["Label"];
int size = inv.Options["Size"];
inv.Options["Weight"] = 24;
Internally, Options reads data from a private Dictionary. Please help me on how I can define the Options property.
Note: This is different than Indexer. With Indexer, I can use below code:
int size = inv["Size"];
But my usage is different.
I found a way to implement it.
public class Options
{
public Dictionary<string, object> _options;
public Options()
{
_options = new Dictionary<string, object>();
}
public object this[string key] {
get { return _options.Single(r => r.Key == key).Value; }
set { _options[key] = value; }
}
}
public class Inventory
{
public Inventory()
{
Options = new Options();
}
public Options Options { get; set; }
}
Usage:
var x = new Inventory();
x.Options["Size"] = 120;
x.Options["Box"] = "4 x 4 x 8";
Console.WriteLine(x.Options["Size"]);
Console.WriteLine(x.Options["Box"]);
I want to create "list of list of list". It should be:
Group (has a list of Members)
Member (has a Name and list of Properties)
Property (has Name and Value)
What I want is to have a possibility to add Property into Member (specified by its name) inside defined Group. Someting like this:
membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).
I have trouble achieving this using list... I found class Hashable, but I am not sure if this is usable... and cannot make it works too...
Thank for any suggestion :)
Well, I suggest you create a custom class instead of your approach. But otherwise you can use a Dictionary.
var properties = new Dictionary<string, string>();
properties.Add("Prop1", "Value");
var members = new Dictionary<string, Dictionary<string, string>>();
members.Add("Member1", properties);
var group = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>();
group.Add("GroupName", members);
public class Group
{
public Group()
{
Members = new List<Member>();
}
public IEnumerable<Member> Members { get; set; }
}
public class Member
{
public Member()
{
Properties = new Dictionary<string, string>();
}
public string Name { get; set; }
IDictionary<string, string> Properties { get; set; }
}
The dictionary can take a key and a value, and the key should be unique.
You can also create a class property if you want to add another thing beside the name and the value
I would use indexers.
Here's a partial implementation:
class Group
{
private List<Member> _members;
public string this
{
get
{
return _members.Find(m => m.Name == value);
}
// You can also implement set here if you want...
}
}
class Member
{
private List<Property> _properties;
public string Name {get;set;}
public string this
{
get
{
return _properties.Find(m => m.Name == value);
}
}
}
class Property
{
public string Name {get;set;}
public string Value {get;set;}
}
And the usage:
var g = new Group();
g[memberName][propertyName].Value = someValue;
Note: This implementation is partial! it still needs constructor logic and any other logic you might need.
Likely the best solution is to use the C# class Dictionary - as suggested by zetawars, or a custom class - as suggested by Zohar Peled, or some mix of the two - as suggested by gandalf.
However, in order to use syntax similar to what is requested in the question...
membersgroup.AddNewMember(memberXYZ);
...
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ).
You can abuse ExpandoObject and Action, and do something awesome like this:
dynamic membersgroup = new ExpandoObject();
var getNewMemberObject = new Func<dynamic>(() =>
{
dynamic memberObject = new ExpandoObject();
var addPropertyAction = new Action<string, string>((propertyName, propertyValue) =>
{
((IDictionary<string, object>)memberObject).Add(propertyName, propertyValue);
});
memberObject.AddProperty = addPropertyAction;
return memberObject;
});
var addNewMemberAction = new Action<string>((memberName) =>
{
((IDictionary<string, object>)membersgroup).Add(memberName, getNewMemberObject());
});
membersgroup.AddNewMember = addNewMemberAction;
string memberXYZ = nameof(memberXYZ);
string nameXYZ = nameof(nameXYZ);
string valueXYZ = nameof(valueXYZ);
// look we did it!
membersgroup.AddNewMember(memberXYZ);
membersgroup.memberXYZ.AddProperty(nameXYZ, valueXYZ);
// and it actually works
var actualValue = membersgroup.memberXYZ.nameXYZ;
Console.WriteLine(actualValue); // "valueXYZ"
(for science of course)
I am fairly new to Reflection, but have been able to retrieve all fields of my passed class. Now I am trying to retrieve the values of each field, but I'm having an issue with List<T>.
I have a simple class for testing:
public class MyTestClass
{
public string Name;
public int Age;
public bool Alive;
public List<int> Counters;
public List<string> People;
public List<Tool> Tools;
public string[] Stuff;
public Tool[] NeededTools;
public MyTestClass(string name, int age, bool alive = true)
{
Name = name;
Age = age;
Alive = alive;
Counters = new List<int>();
Counters.Add(7);
People = new List<string>();
People.Add("Seven");
Tools = new List<Tool>();
Stuff = new string[2];
NeededTools = new Tool[3];
}
}
Here is the code I am using:
private void AttachControl(object source, FieldInfo fi, Control control)
{
switch (fi.FieldType.Name)
{
case "Boolean":
(control.Controls[fi.Name] as ComboBox).SelectedIndex = (fi.GetValue(source).ToString().ToUpper() == "TRUE") ? 1 : 0;
break;
case "List`1":
Control listControl = control.Controls[fi.Name];
var listType = fi.FieldType.GetGenericArguments();
var listFields = listType[0].GetFields(
BindingFlags.Public |
BindingFlags.Instance
);
if (listFields.Length > 0)
{
AttachToControls(listFields, listControl.Controls.Cast<Control>().ToArray());
}
else
{
// *** Here is the issue ***
var values = fi.GetValue(source);
listControl.Controls[fi.Name].Text = values[0].ToString();
}
break;
default:
control.Controls[fi.Name].Text = fi.GetValue(source).ToString();
break;
}
}
When I get to Counters I can retrieve the value var values = fi.GetValue(source); and during debug I can see the List with the value 7 in it, but it states
cannot apply indexing with [] to an expression of type object on the line:
listControl.Controls[fi.Name].Text = values[0].ToString();
I assume I need to cast it, but it will not always be an int type. Do I need to write a section for every type or is there an easier way to accomplish what I need?
FYI - I am writing a Class Library that will allow me to pass any class in and auto create a form to edit all fields.
I'd suggest something along the lines of:
var bob = values as IEnumerable;
listControl.Controls[fi.Name].Text = bob?.Cast<object>()?.FirstOrDefault()?.ToString();
Since the thing you want is a string (not a specific type) then the above code will work fine (assuming values is some form of an enumerable, like a list or an array).
Note, in particular, that IEnumerable interface is this one, not the more commonly used IEnumerable<T>. This allows you to use it without a specific type.
I have a (string, object) dictionary, object (class) has some values including data type which is defined by enum. I need a GetItemValue method that should return dictionary item's value. So return type must be the type which is defined in item object.
Class Item
{
String Name;
DataValueType DataType;
Object DataValue;
}
private Dictionary<string, Item> ItemList = new Dictionary<string, Item>();
void Main()
{
int value;
ItemList.Add("IntItem", new Item("IntItem", DataValueType.TInt, 123));
value = GetItemValue("IntItem"); // value = 123
}
What kind of solution can overcome this problem?
Best Regards,
You can use Generic Classes
Class Item<T>
{
String Name;
T DataTypeObject;
Object DataValue;
public T GetItemValue()
{
//Your code
return DataTypeObject;
}
}
A better solution would be to introduce an interface that you make all the classes implement. Note that the interface doesn't necessarily have to specify any behavior:
public interface ICanBePutInTheSpecialDictionary {
}
public class ItemTypeA : ICanBePutInTheSpecialDictionary {
// code for the first type
}
public class ItemTypeB : ICanBePutInTheSpecialDictionary {
// code for the second type
}
// etc for all the types you want to put in the dictionary
To put stuff in the dictionary:
var dict = new Dictionary<string, ICanBePutInTheSpecialDictionary>();
dict.add("typeA", new ItemTypeA());
dict.add("typeB", new ItemTypeB());
When you need to cast the objects to their specific types, you can either use an if-elseif-block, something like
var obj = dict["typeA"];
if (obj is ItemTypeA) {
var a = obj as ItemTypeA;
// Do stuff with an ItemTypeA.
// You probably want to call a separate method for this.
} elseif (obj is ItemTypeB) {
// do stuff with an ItemTypeB
}
or use reflection. Depending on how many choices you have, either might be preferrable.
If you have a 'mixed bag' you could do something like this...
class Item<T>
{
public String Name { get; set; }
public DataValueType DataType { get; set; }
public T DataValue { get; set; }
}
class ItemRepository
{
private Dictionary<string, object> ItemList = new Dictionary<string, object>();
public void Add<T>(Item<T> item) { ItemList[item.Name] = item; }
public T GetItemValue<T>(string key)
{
var item = ItemList[key] as Item<T>;
return item != null ? item.DataValue : default(T);
}
}
and use it like...
var repository = new ItemRepository();
int value;
repository.Add(new Item<int> { Name = "IntItem", DataType = DataValueType.TInt, DataValue = 123 });
value = repository.GetItemValue<int>("IntItem");
If you have just a couple types - you're better off with Repository<T>.
I found a solution exactly what I want. Thanks to uncle Google.
Thanks all of you for your kind interest.
public dynamic GetValue(string name)
{
if (OpcDataList[name].IsChanged)
{
OpcReflectItem tmpItem = OpcDataList[name];
tmpItem.IsChanged = false;
OpcDataList[name] = tmpItem;
}
return Convert.ChangeType(OpcDataList[name].ItemValue.Value, OpcDataList[name].DataType);
}
public class Stock
{
}
class Program
{
static void Main(string[] args)
{
ObjectCache cache = MemoryCache.Default;
cache["test"] = new Stock();
var x = cache.OfType<Stock>().ToList();
}
}
This is returning empty ...I thought OfType is supposed to return all instances in a collection of type T ?
Just to rule out the ObjectCache as a possible culprit I also tried
List<object> lstTest = new List<object>();
lstTest.Add(new Stock());
var y = lstTest.OfType<Stock>().ToList();
This works however - so it seems like the problem is with the ObjectCache, which is an instance of a Dictionary underneath
SOLUTION
cache.Select(item => item.Value).OfType<T>().ToList()
Thanks Alexei!
MemoryChache returns enumerator of KeyValuePair<string,Object>, not just values: MemoryChache.GetEnumerator().
You need to case accordingly to get your items. Something like:
var y = cache.Select(item => item.Value).OfType<Stock>();
This would work
cache.GetValues(new string[] {"test"}).Values.OfType<Order>()
But I don't think you should use this.
Cache works like a Dictionary...so you can get set of KeyValuePairs with GetValues
This worked for me.
public class Stock
{
public Stock()
{
Name = "Erin";
}
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
System.Collections.ArrayList fruits = new System.Collections.ArrayList(4);
fruits.Add("Mango");
fruits.Add("Orange");
fruits.Add("Apple");
fruits.Add(3.0);
fruits.Add("Banana");
fruits.Add(new Stock());
// Apply OfType() to the ArrayList.
var query1 = fruits.OfType<Stock>();
Console.WriteLine("Elements of type 'stock' are:");
foreach (var fruit in query1)
{
Console.WriteLine(fruit);
}
}
}
Remember IEnumerable is lazily evaluated. Use a foreach to loop through query1 and you will see it only find the Stock object.
Yeah. Sorry myself. ObjectCache is a IEnumerable>
Not really an IDictionary.
This works:
var c = cache.Select(o => o.Value).OfType<Stock>().ToList();