Using Find() to find instance in collection based on enum value - c#

I'm trying to find an object in a collection based on an Enum type property in the object. But it always returns NULL, even though I'm verifying that the collection does contain an object with the expected property value. Why is this?
public enum FooEnum
{
First, Second, Third, Fourth
}
public class Foo
{
public Enum EnumValue { get; set; }
}
class Program
{
static void Main(string[] args)
{
var fooCollection = new List<Foo>();
fooCollection.Add(new Foo() { EnumValue = FooEnum.First });
fooCollection.Add(new Foo() { EnumValue = FooEnum.Second });
fooCollection.Add(new Foo() { EnumValue = FooEnum.Third });
var fooSearchInstance = new Foo() { EnumValue = FooEnum.Fourth };
var fooFoundInstance = fooCollection.Find(f => f.EnumValue == fooSearchInstance.EnumValue); // NULL, for obvious reasons
fooSearchInstance.EnumValue = FooEnum.Second;
fooFoundInstance = fooCollection.Find(f => f.EnumValue == fooSearchInstance.EnumValue); // Also NULL - Why??
}
}

This boxes FooEnum.Second, and stores the resulting reference in fooSearchInstance.Enum:
fooSearchInstance.Enum = FooEnum.Second;
This boxes FooEnum.Second and uses reference identity to compare it with f.Enum:
f.Enum == fooSearchInstance.Enum
You're boxing twice, so you've created separate objects - reference identity won't work for you. The code you've got is a more complicated form of this:
Enum x = FooEnum.Second; // Box once
Enum y = FooEnum.Second; // Box again, to a separate object
Console.WriteLine(x == y); // False, because they're different references
You could use Equals instead:
f => Equals(f.Enum, FooEnum.Second)
... although boxing on every iteration isn't ideal. You could box just once before the Find call:
Enum boxed = FooEnum.Second;
fooFoundInstance = fooCollection.Find(f => Equals(f.EnumValue, boxed));
Even this isn't ideal though, in my opinion. I very rarely find it useful to have a field or property of type Enum. It may be necessary in your case (we don't have enough context to know) but in general it would better to use the concrete enum type if at all possible.

Related

KellermanSoftware CompareNetObjects, comparing collections of different types

I am using CompareNetObjects for automated testing of my system. This has been useful in ensuring performance optimizations do not change the expected behavior of the system, and ensuring errors are not introduced.
When I have a collection that is declared in an abstract way, I expect to have the contents compared, not the collection itself; however, I do not see an option to enable this.
Class:
public class MustBeCorrect
{
public string Name { get; set; }
IEnumerable<string> Items { get; set; }
}
Test:
[Fact] void IsCorrect()
{
var obj1 = new MustBeCorrect
{
Name = "Kitty",
Items = new string[]
{
"Collar",
"Bell"
}
};
var obj2 = new MustBeCorrect
{
Name = "Kitty",
Items = new List<string>
{
"Collar",
"Bell"
}
};
comparer.Compare(obj1, obj2); // False!
}
The above two objects compare as not equal, even though the only difference between the two objects is one uses an array, and the other uses a list. In terms of my contract, however, these two seem as they should be considered equal to me.
How can I configure the comparison options to only compare the contents of collections, rather than the collection itself?
There does not exist a way to do what you want specifically for collections.
However, there is an option you can set called IgnoreObjectTypes in the ComparisonConfig. It is default to false, but if you set it to true, it should give you the behavior you desire for your collections.
Be aware, this will ignore object types for all comparisons.
This is the only way I found to make it work so far.
My solution consists in implementing a Custom comparer that ignores the generic argument of the collection when comparing IEnumerables.
However, the drawback of this approach is that you lose the granularity of the returned result.Differences: if two lists have different elements, it won't tell you exactly how and which elements are different.
If someone could identify a way to implement this in the Custom Comparer, that'd be great.
public class OnlyIEnumContents : BaseTypeComparer
{
public OnlyIEnumContents(RootComparer rootComparer) : base(rootComparer)
{
}
public OnlyIEnumContents() : this(RootComparerFactory.GetRootComparer())
{
}
public override bool IsTypeMatch(Type type1, Type type2)
{
if (typeof(IEnumerable).IsAssignableFrom(type1) && typeof(IEnumerable).IsAssignableFrom(type2))
return true;
return type1 == type2;
}
public override void CompareType(CompareParms parms)
{
var ienum1 = parms.Object1 as IEnumerable;
var ienum2 = parms.Object2 as IEnumerable;
if (ienum1 != null && ienum2 != null)
{
List<object> list1 = new List<object>();
List<object> list2 = new List<object>();
foreach (var item in ienum1)
list1.Add(item);
foreach (var item in ienum2)
list2.Add(item);
// Compare the lists.
// Because all the list elements were boxed in a System.Object,
// this only returns the differences between the single elements.
CompareLogic compareLogic = new CompareLogic();
ComparisonResult result = compareLogic.Compare(list1, list2);
// This is where granularity is lost.
// The official documentation https://github.com/GregFinzer/Compare-Net-Objects/wiki/Custom-Comparers
// points that we should return `AddDifference(parms)`,
// however I don't know how to get `parms` from a given `result` object.
// Returning a single difference for the entire lists.
if (!result.AreEqual)
AddDifference(parms);
}
}
}
And then to use it:
CompareLogic compareLogic = new CompareLogic();
compareLogic.Config.CustomComparers.Add(new OnlyIEnumContents());
ComparisonResult result = compareLogic.Compare(obj1, obj2);

How to access methods of subclass in object array C#?

How can I set/get the value of an object in an object array?
Currently I get:
"object does not contain a definition for 'value' and no extension method"
Example C#;
public class myObjClass
{
public int value = 5;
}
public class myObjClass2
{
public float[] pos = new float[2];
}
public void test()
{
myObjClass myObj = new myObjClass();
myObjClass2 myObj2 = new myObjClass2();
object[] objArr = new object[2];
objArr[0] = myObj;
objArr[1] = myObj2;
Debug.Print(myObj.value.ToString());
Debug.Print(objArr[0].value.ToString()); // how?
}
Its because a generic object does not have the property value your class myObjClass has. To fix this you could cast the item to your class like so:
((myObjClass)objArr[0]).value.ToString()
Only do this ^ if you are sure of the type
Instead you could also check it first:
With as:
var item = objArr[0] as myObjClass;
if( item != null ) // Item will be null if its not a 'myObjClass'
{
//Do stuff with item
}
Or with is:
if( objArr[0] is myObjClass )
{
var item = (myObjClass)objArr[0];
//Do stuff with item
}
When using an object array you have to cast to the real type (here: myObjClass) before accessing the fields:
You can access the object like this
((myObjClass)objArr[0]).value
but I would not recommend. Can´t you have your array to be the concrete type
var array = new myObjClass[42]
A compact safe alternative to retrieve the value is
(objArr[0] as myObjClass)?.value
You need to cast object to known type which is myObjClass, like:
((myObjClass)objArr[0]).value.ToString();
Or you can use reflection
var valueString = objArr[0].GetType().GetProperty("value").GetValue(objArr[0]);
Debug.Print(valueString.ToString());
Hope helps,
Technically you can put it as
Debug.Print((objArr[0] as myObjClass)?.value.ToString());
We try casting objArr[0] as myObjClass and if succeed get value and turn it to string. If objArr[0] is not myObjClass we return null as a string
However, a much better way is to implement ToString() in both classes of interest:
public class myObjClass
{
public int value = 5;
public override string ToString() {
// When debugging we want to know "value"
return value.ToString();
}
}
public class myObjClass2
{
public float[] pos = new float[2];
public override string ToString() {
// When debugging we want to know "pos[0]" and "pos[1]" values
return $"{pos[0]} : {pos[1]}";
}
}
And then put an easy
// Just print out debug info (we don't want to know the actual objArr[0] class)
Debug.Print(objArr[0].ToString());
You have a single object, that indeed is an instance of myObjClass, and has a value field, but you have two references to it.
One (myObj) is known to the compiler to be of type myObjClass, and it can guarantee that it has a value field.
The other (objArr[0]) is only known to the compiler to be of type object, and it cannot guarantee that it has a value field.
For example, you could do:
objArr[0] = (random.Next() > 0.5) : myObj ? myObj2
where we're gonna decide at runtime, based on the value of a random number, which will be the type of the actual object at objArr[0].
So, if this was allowed, half of the time objArr[0].value would be correct, and half of the time it will be an error.

Get and set value from bindinglist<T> with coordinates c#

I'm having trouble getting and setting the values of an item in a bindinglist with coordinates when the type vary.
For example, let's say I have three classes:
public class Client{
public string Name{ get; set; }
}
public class Debt{
public string AccountType{ get; set; }
public int DebtValue { get; set; }
}
public class Accounts{
public string Owner{ get; set; }
public int AccountNumber { get; set; }
public bool IsChekingAccount { get; set; }
}
and then, three bindinglists (imagine they are populated):
public BindingList<Client> listOne;
public BindingList<Debt> listTwo;
public BindingList<Accounts> listThree;
I'm trying to create an extension method that returns an Object with the value requested, or sets the value if it is provided.
public static Object GetValueByCoordinates(this IBindingList list, int x, int y) { /*some magic*/ }
public static Object SetValueByCoordinates(this IBindingList list, int x, int y, Object value) { /*some other magic*/ }
So, for instance, I need to be able to set the value of the item (2,3) in the listThree, and the value (1,1) in listTwo:
listThree.SetValueByCoordinates(2,3,false);
listThree.SetValueByCoordinates(1,1,"My self");
or get the value (1,1) and (2,2) from listOne and listTwo:
string result = listOne.GetValueByCoordinates(1,1).ToString();
intresult = Convert.ToInt32(listOne.GetValueByCoordinates(1,1));
How would you achieve such behavior? i was thinking of using reflection, but I know little to nothing about it.
please note that the methods MUST be called that way, so using something like this must be avoided
public static Object GetValueByCoordinates<T>(this BindingList<T> list, int x, int y) { /*some magic*/ }
Any help will be appreciated.
As mentioned, I am very skeptical that the approach you're asking for help with is likely to be the best or most appropriate way to address whatever the broader issue you're trying to solve is. It can be done (and without very much difficulty), but the resulting code is difficult to maintain, error-prone, and not very readable (which leads to the first two problems).
That said, there are lots of different ways to implement the specific behavior you're asking for. And even if this is not the best way to solve your current problem, the basic techniques are useful to know for other types of problems. With that in mind, here are two of the most obvious ways you might address your problem.
Manually configure a mapping from indexes to getters and setters:
IMHO this is the most preferable way. Not because it's elegant or easy to extend, but specifically because it's not either of those things. Requiring code maintainers to explicitly create the data structure elements to support each type and property that you want to handle will discourage a proliferation of this technique for other related problems, and even for the current problem. It could even encourage someone to spend a little more time thinking about the broader problem so as to find a better strategy.
This approach does have the advantage that it is reasonably performant. Because the code is generated at compile time, the only real overhead is the boxing that occurs for value types. There's some casting but for the reference types that overhead should be practically unmeasurable, and even the boxing overhead may not show up on a profile, depending on how intensively this code might be used.
This particular solution looks like this:
static class ManualIndexedProperty
{
public static void SetValueByCoordinates(this IBindingList list, int x, int y, object value)
{
object o = list[x];
_typeToSetter[o.GetType()][y](o, value);
}
public static object GetValueByCoordinates(this IBindingList list, int x, int y)
{
object o = list[x];
return _typeToGetter[o.GetType()][y](o);
}
private static readonly Dictionary<Type, Func<object, object>[]> _typeToGetter =
new Dictionary<Type, Func<object, object>[]>()
{
{
typeof(Client),
new Func<object, object>[]
{
o => ((Client)o).Name
}
},
{
typeof(Debt),
new Func<object, object>[]
{
o => ((Debt)o).AccountType,
o => ((Debt)o).DebtValue,
}
},
{
typeof(Accounts),
new Func<object, object>[]
{
o => ((Accounts)o).Owner,
o => ((Accounts)o).AccountNumber,
o => ((Accounts)o).IsChekingAccount,
}
},
};
private static readonly Dictionary<Type, Action<object, object>[]> _typeToSetter =
new Dictionary<Type, Action<object, object>[]>()
{
{
typeof(Client),
new Action<object, object>[]
{
(o1, o2) => ((Client)o1).Name = (string)o2
}
},
{
typeof(Debt),
new Action<object, object>[]
{
(o1, o2) => ((Debt)o1).AccountType = (string)o2,
(o1, o2) => ((Debt)o1).DebtValue = (int)o2,
}
},
{
typeof(Accounts),
new Action<object, object>[]
{
(o1, o2) => ((Accounts)o1).Owner = (string)o2,
(o1, o2) => ((Accounts)o1).AccountNumber = (int)o2,
(o1, o2) => ((Accounts)o1).IsChekingAccount = (bool)o2,
}
},
};
}
Two dictionaries are declared, one each for setting and getting property values. The dictionaries map the element object's type to an array of delegate instances to perform the actual work. Each delegate instance references an anonymous method which has been hand-coded to perform the necessary operation.
One major advantage to this approach is that it is explicit and obvious what index corresponds to what property for each type.
This approach will be tedious and time-consuming to set up if you are dealing with any significant number of types and/or properties. But IMHO that's a good thing. As I noted above, hopefully the pain of this approach can help convince someone to abandon the idea of accessing the properties by index altogether. :)
If this kind of tedium is unacceptable and yet you still insist on the indexed-property-access approach, then you can in fact use reflection as an alternative…
Use reflection to access the properties:
This technique is more dynamic. Once implemented, it works for any type object without modification, and does not require additional work to support new types.
One major disadvantage is that in order to produce consistent, predictable results, it sorts the properties by name. This ensures that changes in the C# compiler and/or CLR won't break the code, but it means you can't add or remove properties from a type without updating the code that is accessing those properties by index.
In my demo usage code (see further below), I address this maintenance issue by declaring enum types that provide int values for property names. This would be a good way to help reduce the maintenance overhead if the code is actually referring to the properties with literal index values.
However, it's possible your scenario involves dynamically accessing the property values by index, e.g. in a serialization scenario or similar. In that case, you will also need to add something that can remap or otherwise deal with changes in the index values should properties be added or removed to the types.
Frankly, either way this issue of the types indexes changing is one big reason I'd strongly recommend against this indexed access to properties in the first place. But again, if you insist…
static class ReflectionIndexedProperty
{
public static void SetValueByCoordinates(this IBindingList list, int x, int y, object value)
{
object o = list[x];
GetProperty(o, y).SetValue(o, value);
}
public static object GetValueByCoordinates(this IBindingList list, int x, int y)
{
object o = list[x];
return GetProperty(o, y).GetValue(o);
}
private static PropertyInfo GetProperty(object o, int index)
{
Type type = o.GetType();
PropertyInfo[] properties;
if (!_typeToProperty.TryGetValue(type, out properties))
{
properties = type.GetProperties();
Array.Sort(properties, (p1, p2) => string.Compare(p1.Name, p2.Name, StringComparison.OrdinalIgnoreCase));
_typeToProperty[type] = properties;
}
return properties[index];
}
private static readonly Dictionary<Type, PropertyInfo[]> _typeToProperty = new Dictionary<Type, PropertyInfo[]>();
}
In this version, the code retrieves the array of PropertyInfo objects for a given type, sorts that array by name, retrieves the appropriate PropertyInfo object for the given index, and then uses that PropertyInfo object to set or get the property value, as appropriate.
Reflection incurs a significant run-time performance overhead. This particular implementation mitigates some of that overhead by caching the sorted arrays of PropertyInfo objects. That way, they only need to be created once, the first time the code has to handle an object of the given type.
Demo code:
As I mentioned, to make it easier to compare the two approaches without having to go to each method call and hand-change an integer literal used for the call, I created some simple enum types to represent the property indexes. I also wrote some code to initialize some lists that could be tested.
Note: one very important thing to point out is that in your question you were not very consistent about how you were indexing the properties. In my code example, I have chose to stick with a 0-based index (consistent with the natural indexing used in C# arrays and other collections). You can of course use a different base (e.g. 1-based indexing), but you will need to make sure you are entirely consistent throughout the code (including subtracting 1 from the passed-in index when actually indexing an array).
My demo code looks like this:
class Program
{
static void Main(string[] args)
{
BindingList<Client> listOne = new BindingList<Client>()
{
new Client { Name = "ClientName1" },
new Client { Name = "ClientName2" },
new Client { Name = "ClientName3" },
};
BindingList<Debt> listTwo = new BindingList<Debt>()
{
new Debt { AccountType = "AccountType1", DebtValue = 29 },
new Debt { AccountType = "AccountType2", DebtValue = 31 },
new Debt { AccountType = "AccountType3", DebtValue = 37 },
};
BindingList<Accounts> listThree = new BindingList<Accounts>()
{
new Accounts { Owner = "Owner1", AccountNumber = 17, IsChekingAccount = false },
new Accounts { Owner = "Owner2", AccountNumber = 19, IsChekingAccount = true },
new Accounts { Owner = "Owner3", AccountNumber = 23, IsChekingAccount = true },
};
LogList(listThree);
listThree.SetValueByCoordinates(2, (int)AccountsProperty.IsChekingAccount, false);
listThree.SetValueByCoordinates(1, (int)AccountsProperty.Owner, "My self");
LogList(listThree);
string result1 = (string)listOne.GetValueByCoordinates(0, (int)ClientProperty.Name);
int result2 = (int)listTwo.GetValueByCoordinates(1, (int)DebtProperty.DebtValue);
LogList(listOne);
LogList(listTwo);
Console.WriteLine("result1: " + result1);
Console.WriteLine("result2: " + result2);
}
static void LogList<T>(BindingList<T> list)
{
foreach (T t in list)
{
Console.WriteLine(t);
}
Console.WriteLine();
}
}
Note that I use simple casting to convert from object to the specific type, both with setting property values and getting them. This is a much better approach than e.g. calling ToString() or Convert.ToInt32(); you know exactly what the type is supposed to be, and it's either an actual instance of that type (for reference types) or a boxed instance (for value types), and either way a cast does exactly what you need.
I also added ToString() overrides to your example classes to make it easier to see the output:
public class Client
{
public string Name { get; set; }
public override string ToString()
{
return "{" + Name + "}";
}
}
public class Debt
{
public string AccountType { get; set; }
public int DebtValue { get; set; }
public override string ToString()
{
return "{" + AccountType + ", " + DebtValue + "}";
}
}
public class Accounts
{
public string Owner { get; set; }
public int AccountNumber { get; set; }
public bool IsChekingAccount { get; set; }
public override string ToString()
{
return "{" + Owner + ", " + AccountNumber + ", " + IsChekingAccount + "}";
}
}
Finally, here are the enum declarations used:
Manual indexing:
enum ClientProperty
{
Name = 0
}
enum DebtProperty
{
AccountType = 0,
DebtValue = 1
}
enum AccountsProperty
{
Owner = 0,
AccountNumber = 1,
IsChekingAccount = 2,
}
Reflection/sorted by name:
enum ClientProperty
{
Name = 0
}
enum DebtProperty
{
AccountType = 0,
DebtValue = 1
}
enum AccountsProperty
{
AccountNumber = 0,
IsChekingAccount = 1,
Owner = 2,
}
Of course, these could both have been the same values. That is, while you don't have control over the sort order, once the property names are given, the manual version could have declared the manually-written lambdas in sorted-by-name order so that the same indexes would have worked either way. It doesn't matter too much what you decide to do; it just has to be consistent.
Final thoughts…
Have I mentioned yet how strongly I would recommend against building any significant amount of code around this technique? It's not at all clear what your actual bigger-picture problem you're trying to solve is, but there are just a lot of different ways for this to go wrong, and it is likely to lead to lots of hard-to-find, time-consuming-to-fix bugs in the code.
In terms of performance, the above should not be too bad as long as you are not executing the code in a tight loop for huge numbers of objects and property values. The manual (first) example in particular should be relatively fast. It is possible to achieve the generalized design of the reflection-based approach with the minimal overhead of the manual approach by using the Expression type. That's a bit more complicated, but would have the advantage that you can generate expressions dynamically that wind up being effectively the compiled-code implementation of the manual approach.

Store object string reference

I think this question was asked many times in C# but my problem is maybe more solvable.
I have an object value as a string : myobject.value and I want to store this value in a queue (or anything else) to access it later. Is it possible?
I read a lot of posts saying that it is not possible to store a ref to a string.
I don't see any solution to store a ref to my string value myobject.value and change it later.
Any Ideas ?
If you want to store a reference to a string that people can see and share the changes of you will need to wrap it in a class:
public class Wrapper<T>
{
public T Item { get; set; }
}
Then people use this instead of a string:
class MyClass
{
public Wrapped<string> SharedString { get; set; }
}
var c1 = new MyClass();
var c2 = new MyClass();
var s = new Wrapped<string> { Item = "Hello" };
c1.SharedString = s;
c2.SharedString = s;
c1.SharedString.Item = "World";
Console.Writeline(c2.SharedString.Item);
Even though strings are reference types, they are immutable so changes need to the "copied" around. Sharing is this way doesn't change the immutability, it just centrally holds one copy of a string that everyone is looking at via their reference to Wrapped<string>.
You can take the Wrapped<T> class further and give it implicit cast support to and from T, for a little syntactic sugar:
public static implicit operator T(Wrapped<T> wrapper)
{
return wrapper.Item;
}
public static implicit operator Wrapped<T>(T item)
{
return new Wrapped<T> { Item = item };
}
Wrapped<int> i = 2;
int j = i;
// Careful, this is a re-assignment of s, not a change of s.Item.
Wrapped<string> s = "Hello";
If I understand you right, and myobject.value is a string, than you can certainly store a collection representing those values
List<string> strLst = new List<string>();
strLst.Add(`myobject.value`);
No, you can't store a reference as a string, nor should you have to, but you can make a string(or xml) that represents all the information about your object, if you really need to.

How do I compare FieldInfo's values of instances?

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myClass instance1 = new myClass();
myClass instance2 = new myClass();
FieldInfo[] fields = typeof(myClass).GetFields();
foreach (FieldInfo field in fields) if (field.GetValue(instance2) == field.GetValue(instance1)) Text = "Yes";
}
}
class myClass
{
public bool b = false;
public int i = 2;
}
Never returns "Yes".
EDIT: Without knowing beforehand what the types will be. So I can't have: (bool)field.GetValue(instance1).
You're using ==, which will be comparing the boxed values for any field where the type is a value type. Each time a value is boxed, it will create a new object, so == will never work like that. Use object.Equals instead:
foreach (FieldInfo field in fields)
{
if (object.Equals(field.GetValue(instance2), field.GetValue(instance1))
{
Text = "Yes";
}
}
(Using the static method here means it'll work even if the values are null.)
you are comparing the address of the two objects returned by FieldInfo.GetValue and since those addresses in memory are different, the == is never true.
try replacing the if with this:
if (field.GetValue(instance2).Equals(field.GetValue(instance1)))
Because field.GetValue(instance1) returns a "boxed" (object) version of the value, hence calling == you are only comparing two different references.
Try instead calling:
field.GetValue(instance2).Equals(field.GetValue(instance1))

Categories

Resources