C# how to set parameter value by Func Delegate - c#

I need a clean way to update an object using parameters of same class.
I am defining list of fields as Func<T, object> delegates. These are lists that should be compared and updated if nessecary. Unfortunately I can't figure out a clean way to implement it.
Following code doesn't work:
public class UpdatableClass
{
public int Id { get; set; }
public int IntValue { get; set; }
public string StringValue { get; set; }
public DateTime ModifiedDate { get; set; }
private List<Func<UpdatableClass, object>> UpdatableFields =
new List<Func<UpdatableClass, object>>()
{
c => c.IntValue,
c => c.StringValue
};
public bool Update(UpdatableClass newValues)
{
bool isUpdated = false;
foreach (var fieldSelector in UpdatableFields)
{
object oldValue = fieldSelector(this);
object newValue = fieldSelector(newValues);
if (!newValue.Equals(oldValue))
{
oldValue = newValue;
isUpdated = true;
}
}
return isUpdated;
}
}
[TestFixture]
public class UpdatableClassTests
{
[Test]
public void TestUpdateMethod()
{
UpdatableClass oldObject = new UpdatableClass()
{
StringValue = "OldString",
IntValue = 3,
};
bool isUpdated = oldObject.Update(new UpdatableClass() { StringValue = "NewString", IntValue = 4 });
Assert.IsTrue(isUpdated);
Assert.AreEqual("NewString", oldObject.StringValue);
Assert.AreEqual(4, oldObject.IntValue);
}
}

This code can be used as possible solution for the problem. Instead of get only Func<T, object> you can use a tuple for both getter and setter (Func<UpdatableClass, object> Get, Action<UpdatableClass, object> Set). I don't think that it's the best solution, but it resolves question and make test passing
public class UpdatableClass
{
public int Id { get; set; }
public int IntValue { get; set; }
public string StringValue { get; set; }
public DateTime ModifiedDate { get; set; }
private List<(Func<UpdatableClass, object> Get, Action<UpdatableClass, object> Set)> UpdatableFields =
new List<(Func<UpdatableClass, object>, Action<UpdatableClass, object>)>
{
(c => c.IntValue, (c, v) => { c.IntValue = Convert.ToInt32(v); }),
(c => c.StringValue, (c, v) => { c.StringValue = v.ToString(); })
};
public bool Update(UpdatableClass newValues)
{
bool isUpdated = false;
foreach (var field in UpdatableFields)
{
object oldValue = field.Get(this);
object newValue = field.Get(newValues);
if (!newValue.Equals(oldValue))
{
field.Set(this, newValue);
isUpdated = true;
}
}
return isUpdated;
}
}

Related

C# combining multiple filter bool methods into one

I've created four methods to get working filter based on multiple parameters:
bool FilterAreas(AreaPlaceCoordinate apc)
{
if (!AreaHash.Any())
{
return true;
}
foreach (var _ in AreaHash.Where(h => apc.AreaName.Contains(h, StringComparison.OrdinalIgnoreCase)).Select(h => new { }))
{
return true;
}
return false;
}
bool FilterPlaces(AreaPlaceCoordinate apc)
{
if (!PlaceHash.Any())
{
return true;
}
foreach (var _ in PlaceHash.Where(h => apc.PlaceName.Contains(h, StringComparison.OrdinalIgnoreCase)).Select(h => new { }))
{
return true;
}
return false;
}
bool FilterCoordinates(AreaPlaceCoordinate apc)
{
if (!CoordinateHash.Any())
{
return true;
}
foreach (var _ in CoordinateHash.Where(h => apc.CoordinateName.Contains(h, StringComparison.OrdinalIgnoreCase)).Select(h => new { }))
{
return true;
}
return false;
}
bool Filter(AreaPlaceCoordinate apc)
{
return FilterCoordinates(apc)&&FilterPlaces(apc)&& FilterAreas(apc);
}
I think it could be done with one method, but despite of many attempts I don't know how to handle it. Those are my objects used in example:
//Hash
private IEnumerable<string> PlaceHash { get; set; } = new HashSet<string>() { };
private IEnumerable<string> AreaHash { get; set; } = new HashSet<string>() { };
private IEnumerable<string> CoordinateHash { get; set; } = new HashSet<string>() { };
public class AreaPlaceCoordinate
{
public int CoordinateId { get; set; }
public string CoordinateName { get; set; }
public int AreaId { get; set; }
public int PlaceId { get; set; }
public string AreaName { get; set; }
public string PlaceName { get; set; }
}
I would appreciate any ideas
Update
Thank you, #Abanslash!
I also added string text filter and ended with code like this:
private string searchString1 = "";
static bool FilterHash<T>(IEnumerable<string> list, T viewModel, Func<string, bool> p)
{
if (!list.Any())
{
return true;
}
return list.Any(p);
}
public bool TestFilter(AreaPlaceCoordinate apc)
{
bool textEmpty = (string.IsNullOrWhiteSpace(searchString1));
List<string> names = new() { apc.AreaName, apc.PlaceName, apc.CoordinateName };
bool filterText = textEmpty ? true : names.Any(n => n.Contains(searchString1, StringComparison.OrdinalIgnoreCase));
bool areaSelector(string str) => apc.AreaName.Contains(str);
bool coordinateSelector(string str) => apc.CoordinateName.Contains(str);
bool placeSelector(string str) => apc.PlaceName.Contains(str);
return FilterHash(AreaHash, apc, areaSelector) && FilterHash(CoordinateHash, apc, coordinateSelector) && FilterHash(PlaceHash, apc, placeSelector) && filterText;
}
I believe what you want is to use are Predicates, you can have a generic method where you also pass the predicate.
https://learn.microsoft.com/en-us/dotnet/api/system.predicate-1?view=net-6.0
If you are not very familiar with generic you can also specify the base type of the generic using where.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint
In case you were not able to solve your issue with my previous comment here you go:
bool Filter<T>(IEnumerable<string> list, T apc, Func<string, bool> p)
{
if (!list.Any())
{
return true;
}
return list.Any(p);
}
public void testFilter()
{
AreaPlaceCoordinate coord = new AreaPlaceCoordinate();
Func<string, bool> selector = str => coord.AreaName.Contains(str);
this.Filter(PlaceHash, coord, selector);
}
You will have to modify a little bit for what you want to achieve and create the different selectors for your different filters.

C# LINQ, dynamic grouping by [Key] attributes

Consider the following classes:
public class Potato
{
[Key]
public string Farm { get; set; }
[Key]
public int Size { get; set; }
public string Trademark { get; set; }
}
public class Haybell
{
[Key]
public string color { get; set; }
public int StrawCount { get; set; }
}
public class Frog
{
[Key]
public bool IsAlive { get; set; }
[Key]
public bool IsVirulent { get; set; }
public byte LimbCount { get; set; } = 4;
public ConsoleColor Color { get; set; }
}
Each class has properties with [Key] attribute. Is it possible to dynamically group an IEnumerable of any of these classes by their respective [Key] attributes?
I would go for adding extension methods for each your types, like
Option 1:
static class Extensions
{
public static IEnumerable<IGrouping<Tuple<string, int>, Potato>>
GroupByPrimaryKey(this IEnumerable<Potato> e)
{
return e.GroupBy(p => Tuple.Create(p.Farm, p.Size));
}
public static IEnumerable<IGrouping<Tuple<bool, bool>, Frog>>
GroupByPrimaryKey(this IEnumerable<Frog> e)
{
return e.GroupBy(p => Tuple.Create(p.IsAlive, p.IsVirulent));
}
}
If there are lots of types, you may generate the code using t4.
Usage: .GroupByPrimaryKey().
Option 2:
A simpler variation:
static class Extensions
{
public static Tuple<string, int> GetPrimaryKey(this Potato p)
{
return Tuple.Create(p.Farm, p.Size);
}
public static Tuple<bool, bool> GetPrimaryKey(this Frog p)
{
return Tuple.Create(p.IsAlive, p.IsVirulent);
}
}
Usage: .GroupBy(p => p.GetPrimaryKey()).
Option 3:
A solution with reflection is possible, but will be slow. Sketch (far from production-ready!)
class CombinedKey : IEquatable<CombinedKey>
{
object[] _keys;
CombinedKey(object[] keys)
{
_keys = keys;
}
public bool Equals(CombinedKey other)
{
return _keys.SequenceEqual(other._keys);
}
public override bool Equals(object obj)
{
return obj is CombinedKey && Equals((CombinedKey)obj);
}
public override int GetHashCode()
{
return 0;
}
public static CombinedKey GetKey<T>(T instance)
{
return new CombinedKey(GetKeyAttributes(typeof(T)).Select(p => p.GetValue(instance, null)).ToArray());
}
private static PropertyInfo[] GetKeyAttributes(Type type)
{
// you definitely want to cache this
return type.GetProperties()
.Where(p => Attribute.GetCustomAttribute(p, typeof(KeyAttribute)) != null)
.ToArray();
}
}
Usage: GroupBy(p => CombinedKey.GetKey(p))
The challenge here is that you need to build an anonymous type in order to have a GroupBy Expression that can translate to SQL or any other LINQ provider.
I'm not sure that you can do that using reflection (not without some really complex code to create an anonymous type at runtime). But you could create the grouping expression if you were willing to provide an example of the anonymous type as the seed.
public static Expression<Func<TSource, TAnon>> GetAnonymous<TSource,TAnon>(TSource dummy, TAnon example)
{
var ctor = typeof(TAnon).GetConstructors().First();
var paramExpr = Expression.Parameter(typeof(TSource));
return Expression.Lambda<Func<TSource, TAnon>>
(
Expression.New
(
ctor,
ctor.GetParameters().Select
(
(x, i) => Expression.Convert
(
Expression.Property(paramExpr, x.Name), // fetch same named property
x.ParameterType
)
)
), paramExpr);
}
And here's how you would use it (Note: the dummy anonymous type passed to the method is there in order to make the anonymous type a compile-time Type, the method doesn't care what the values are that you pass in for it.) :
static void Main()
{
var groupByExpression = GetAnonymous(new Frog(), new {IsAlive = true, IsVirulent = true});
Console.WriteLine(groupByExpression);
var frogs = new []{ new Frog{ IsAlive = true, IsVirulent = false}, new Frog{ IsAlive = false, IsVirulent = true}, new Frog{ IsAlive = true, IsVirulent = true}};
var grouped = frogs.AsQueryable().GroupBy(groupByExpression);
foreach (var group in grouped)
{
Console.WriteLine(group.Key);
}
}
Which produces:
Param_0 => new <>f__AnonymousType0`2(Convert(Param_0.IsAlive, Boolean), Convert(Param_0.IsVirulent, Boolean))
{ IsAlive = True, IsVirulent = False }
{ IsAlive = False, IsVirulent = True }
{ IsAlive = True, IsVirulent = True }
Somebody had posted a valid answer and removed it later for some reason. Here it is:
Combined key class:
class CombinedKey<T> : IEquatable<CombinedKey<T>>
{
readonly object[] _keys;
public bool Equals(CombinedKey<T> other)
{
return _keys.SequenceEqual(other._keys);
}
public override bool Equals(object obj)
{
return obj is CombinedKey<T> key && Equals(key);
}
public override int GetHashCode()
{
int hash = _keys.Length;
foreach (object o in _keys)
{
if (o != null)
{
hash = hash * 13 + o.GetHashCode();
}
}
return hash;
}
readonly Lazy<Func<T, object[]>> lambdaFunc = new Lazy<Func<T, object[]>>(() =>
{
Type type = typeof(T);
var paramExpr = Expression.Parameter(type);
var arrayExpr = Expression.NewArrayInit(
typeof(object),
type.GetProperties()
.Where(p => (Attribute.GetCustomAttribute(p, typeof(KeyAttribute)) != null))
.Select(p => Expression.Convert(Expression.Property(paramExpr, p), typeof(object)))
.ToArray()
);
return Expression.Lambda<Func<T, object[]>>(arrayExpr, paramExpr).Compile();
}, System.Threading.LazyThreadSafetyMode.PublicationOnly);
public CombinedKey(T instance)
{
_keys = lambdaFunc.Value(instance);
}
}
Caller function and the actual usage:
public static class MyClassWithLogic
{
//Caller to CombinedKey class
private static CombinedKey<Q> NewCombinedKey<Q>(Q instance)
{
return new CombinedKey<Q>(instance);
}
//Extension method for IEnumerables
public static IEnumerable<T> DistinctByPrimaryKey<T>(this IEnumerable<T> entries) where T : class
{
return entries.AsQueryable().GroupBy(NewCombinedKey)
.Select(r => r.First());
}
}
Yes, it is relatively slow, so if it is a problem, then Klaus Gütter's solutions are the way to go.

how to get all the properties and values from the generic parameter

I want to compare all of my list of generic T property value to my local variable searchText.
i already try:
x.GetType().GetProperties().All(props => props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower()))
and i get error : NullReferenceException: Object reference not set to an instance of an object.
here my full method :
protected List<T> ProcessCollection<T>(List<T> lstElements, IFormCollection requestFormData, Func<T, IComparable> getProp)
{
string searchText = string.Empty;
Microsoft.Extensions.Primitives.StringValues tempOrder = new[] { "" };
if (requestFormData.TryGetValue("search[value]", out tempOrder))
{
searchText = requestFormData["search[value]"].ToString();
}
var skip = Convert.ToInt32(requestFormData["start"].ToString());
var pageSize = Convert.ToInt32(requestFormData["length"].ToString());
if (requestFormData.TryGetValue("order[0][column]", out tempOrder))
{
var columnIndex = requestFormData["order[0][column]"].ToString();
var sortDirection = requestFormData["order[0][dir]"].ToString();
tempOrder = new[] { "" };
if (requestFormData.TryGetValue($"columns[{columnIndex}][data]", out tempOrder))
{
var columName = requestFormData[$"columns[{columnIndex}][data]"].ToString();
if (pageSize > 0)
{
var prop = GetProperty<T>(columName);
if (sortDirection == "asc")
{
return lstElements.Where(x=>x.GetType().GetProperties().All(props => props.GetValue(x).ToString().ToLower().Contains(searchText.ToLower()))
.Skip(skip).Take(pageSize).OrderBy(b => prop.GetValue(b)).ToList();
}
else
return lstElements
.Where(x => getProp(x).ToString().ToLower().Contains(searchText.ToLower()))
.Skip(skip).Take(pageSize).OrderByDescending(b => prop.GetValue(b)).ToList();
}
else
return lstElements;
}
}
return null;
}
this is how i call my method :
var listItem = ProcessCollection<Jtabel>(temp,requestFormData,x=>x.Name);
the temp that i pass to method already filled
and this is the class type that i pass to this method
public class Jtabel : BaseModel
{
public string Creator { get; set; }
public string Name { get; set; }
public int SO { get; set; }
public int SOD { get; set; }
public int SAP { get; set; }
public int BA { get; set; }
public int Invoice { get; set; }
public int EPD { get; set; }
public double DP { get; set; }
}
i will try to explain more detail as much i can, so i want to get all of my property from Jtabel class to my ProcessCollection where inside the method all of Jtable property will test each element that's contain searchText.

c# reflection comparing 2 objects with boolean properties

I have two objects with several properties that are of type boolean.
The properties are identical in terms of naming and I just want to make sure they are equal. I do a lot of these checks and want to know if anyone has any suggestion of a method that can do the following code in one step but is dynamic enough to check the property options by name if they have the same name?
if (options != null && other != null)
return options.Quantities == other.Quantities &&
options.SKUEntityKey == other.SKUEntityKey &&
options.LineItemType_Type == other.LineItemType_Type &&
options.SKUIdentifier == other.SKUIdentifier &&
options.Identifier == other.Identifier;
If what I'm asking isn't clear please let me know
class Program
{
static void Main(string[] args)
{
ReflectOverProperties<TestClass, TestClass2>(new TestClass(), new TestClass2());
Console.ReadLine();
}
public static void ReflectOverProperties<T, Z>(T x, Z y)
{
var properties = typeof(T).GetProperties();
foreach (var item in properties)
{
CompareProperty(x, y, item.Name);
}
}
private static void CompareProperty<T, Z>(T x, Z y, string itemName)
{
dynamic originalValue = GetPropValue(x, itemName);
dynamic newValue = GetPropValue(y, itemName);
PropertyCompare(itemName, originalValue, newValue);
}
private static void PropertyCompare(string itemName, dynamic originalValue, dynamic newValue)
{
if (originalValue != newValue)
{
Console.Write($"Property {itemName} does not match");
}
}
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
}
public class TestClass
{
public TestClass()
{
Test1 = false;
Test2 = false;
Test3 = false;
}
public bool Test1 { get; set; }
public bool Test2 { get; set; }
public bool Test3 { get; set; }
}
public class TestClass2
{
public TestClass2()
{
Test1 = false;
Test2 = false;
Test3 = true;
}
public bool Test1 { get; set; }
public bool Test2 { get; set; }
public bool Test3 { get; set; }
}
Here is some Code I modified pretty quick, which runs in console app. Hopefully set you in the right direction, was doing the same object now can be different. Just using some basic reflection and dynamic properties.
I hope this terrible console app will at least give an idea of how to go about doing what you want:
static void Main(string[] args)
{
Person steve = new Person()
{
IsHungry = true,
IsLazy = false,
IsSleepy = true
};
Dog bella= new Dog()
{
IsHungry = true,
IsLazy = false,
IsSleepy = true
};
bool match = DoAllBoolPropertiesMatch(steve, bella);
Console.WriteLine($"\r\n----> Do Bools in Objects Match?: {match}");
}
private static bool DoAllBoolPropertiesMatch(object obj1, object obj2)
{
// For each Boolean property of object 1, check object 2:
foreach(PropertyInfo propInfo in obj1.GetType().GetProperties())
{
// Property is boolean.
if(propInfo.PropertyType == typeof(Boolean))
{
// Look for a property on obj2 with the same name that also returns a bool.
PropertyInfo matchingPropInfo = obj2.GetType().GetProperty(propInfo.Name, typeof(Boolean));
if(matchingPropInfo != null)
{
Console.WriteLine($"Evaluating Property {propInfo.Name} from obj1:");
Console.WriteLine($" - Value for Obj1 = [{propInfo.GetValue(obj1)}]");
Console.WriteLine($" - Value for Obj2 = [{matchingPropInfo.GetValue(obj2)}]");
if(Convert.ToBoolean(propInfo.GetValue(obj1)) != Convert.ToBoolean(matchingPropInfo.GetValue(obj2)))
return false;
}
}
}
return true;
}
public class Person
{
public bool IsHungry { get; set; }
public bool IsSleepy { get; set; }
public bool IsLazy { get; set; }
}
public class Dog
{
public bool IsHungry { get; set; }
public bool IsSleepy { get; set; }
public bool IsLazy { get; set; }
}

Is it possible to set the property of the condition dynamically?

Normally when I want for example to find the first or default item of a List I use this way:
myList.SingleOrDefault(x=>x.MyPropery01 == "myCondition");
However, I would like to know if it is possible, for example by reflection, if I set the the property MyProperty dynamically, something like:
myList.SingleOrDefault(x=>x.GetType().GetProperty("MyProperty01") == "myCondition");
Because sometimes I need to search for MyProperty01, sometimes for MyProperty02, MyProperty03, etc..
EDIT: in visual studio I get this error:
"Operator '==' can't be applied to operands of type System.reflection.PropertyInfo and string".
Yeah you can do that. You were pretty close, here is a demo you can drop in linqpad. Note that the important part is
Single(l => l.GetType().GetProperty(prop).GetValue(l).ToString() == "Condition")
void Main()
{
var myList = Enumerable.Range(0,10).Select(i => new Xmas(i,"name"+i)).ToList();
string prop = "name";
Console.WriteLine(myList.Single(l => l.GetType().GetProperty(prop).GetValue(l).ToString() == "name6").name);
}
public class Xmas
{
public int id { get; set; }
public string name { get; set; }
public Xmas( int id, string name )
{
this.id = id;
this.name = name;
}
}
Working example:
public class Apple
{
public string Color { get; set; }
}
public List<Apple> ApplesList {get;set;}
public void Process()
{
PropertyInfo pi = typeof(Apple).GetProperty("Color");
ApplesList = ApplesList.Where(r => (string)pi.GetValue(r) == "Red").ToList();
}
You could also write an Extension method, that allow to get the property on every object, returning null when it doesn't exist, or doesn't have a GetMethod. You could keep a Cache if you want
public static class ObjectExtension
{
static IDictionary<KeyValuePair<Type, string>, PropertyInfo> propertyCache = new Dictionary<KeyValuePair<Type, string>, PropertyInfo>();
public static object GetProperty(this object source, string propertyName, bool useCache = true)
{
if (source == null)
{
return null;
}
var sourceType = source.GetType();
KeyValuePair<Type, string> kvp = new KeyValuePair<Type, string>(sourceType, propertyName);
PropertyInfo property = null;
if (!useCache || !propertyCache.ContainsKey(kvp))
{
property = sourceType.GetProperty(propertyName);
if (property == null)
{
return null;
}
var get = property.GetGetMethod();
if (get == null)
{
return null;
}
if (useCache)
{
propertyCache.Add(kvp, property);
}
}
else
{
property = propertyCache[kvp];
}
return property.GetValue(source, null);
}
public static T GetProperty<T>(this object source, string propertyName)
{
object value = GetProperty((object)source, propertyName);
if (value == null)
{
return default(T);
}
return (T)value;
}
}
A small test class could then be:
public class Item
{
public string MyProperty { get; set; }
public string MyProperty3 { get; set; }
public string MyProperty2 { protected get; set; }
public Item()
{
MyProperty = "Test propery";
MyProperty3 = "Test property 3";
MyProperty2 = "Yoohoo";
}
}
With a main class for testing
class Program
{
static void Main(string[] args)
{
Item item = new Item();
for (int x = 0; x < 4; x++)
{
string key = "MyProperty" + (x > 0 ? x.ToString() : "");
string value = item.GetProperty<string>(key);
Console.WriteLine("Getting {0} = {1}", key, value);
}
Console.ReadLine();
}
}
which gives the expectable output of:
Getting MyProperty = Test propery
Getting MyProperty1 =
Getting MyProperty2 =
Getting MyProperty3 = Test property 3

Categories