Accessing class fields throughout generic function (.Net Framework 4.8) - c#

First, I have this function that return true if the name is in the class:
public bool hasName<T>(List<T> Data, string name, Func<T, string> ClassName)
{
foreach (T entry in Data)
{
if (ClassName(entry) == name)
{
return true;
}
}
return false;
}
And it's called using:
hasName(Data, name, x => x.name)
The problem is, that i have another function that uses HasName but doesn't know about the field name.
public List<T> MergeClasses<T>(List<T> Pri, List<T> Sec, Func<T, string> ClassName)
{
List<T> result = new List<T>();
result.AddRange(Pri);
foreach (T entry in Sec)
{
if (!new Functions().hasName(result, ClassName(entry), x => x.name))
{
result.Add(entry);
}
}
return result;
}
How can i solve this?

You would need an interface or base-class to use as a generic constraint; for example:
interface IHazHame {
string Name {get;} // note property, not field
}
then your type with .name would need to implement that:
class Foo : IHazName {
// ...
}
and you can restrict your generic method to T that satisfy this:
public List<T> MergeClasses<T>(List<T> Pri, List<T> Sec, Func<T, string> ClassName)
where T : IHazHame
{
// ... x => x.Name
}

Related

Build dynamic predicate based on generic type

How do I make this expression dynamic based on the generic type passed in the parameter?
In the simplified form:
public static class CompareService
{
public static List<T> Run<T>(List<T> database_list, string directory_path)
{
var csv_list = CompareService.MergeRecordsFromFiles<T>(directory);
return CompareService.RunComparison<T>(database_list, csv_list);
}
public static T CompareData<T>(List<T> database_list, List<T> csv_list)
{
var diff = new List<T>();
foreach (var db_item in database_list)
{
// ...
// if T is of type Deathstar compare reference_number property
// if T is of type Stormtrooper compare id property
// if T is of type Sith compare id and anger_level property
var csv_item = csv_list.FirstOrDefault(x => x.reference_number == db_item.reference_number);
// Comparison code
ComparisonResult result = compareLogic.Compare(db_item, csv_item);
// ...
}
return diff;
}
}
It is called from another generic service:
public static void Whatever<T>(List<T> list)
{
// ...
var directory_path = "C:\";
var delta = CompareService.CompareData<T>(list, directory_path);
// ...
}
The most naive implementation would be to check if your itemToFind can be cast to DeathStar, StormTrooper or Sith and if so call the instances property.
var deathStar = itemToFind as DeathStar;
if(deathStar != null)
return database_list.Where(x => ((DeathStar)x).reference_number == deathStar.reference_number).FirstOrDefault();
else
{
var sith = itemToFind as Sith;
if(sith != null)
return database_list.Where(x => ((Sith)x).anger_level == sith.anger_level).FirstOrDefault();
else
return database_list.Where(x => ((StormTrooper)x).id== ((StormTrooper)item).id).FirstOrDefault();
}
This is quite cumbersome, including many casts. In particular it completely bypasses the actual benefits of generics using any arbitrary type (that fullfills the constraints if existing). In your case you´d have a generic method that will only wortk for three decent types.
A better approach is to let all your classes implement a common interface that defines a property, for instance:
interface IObject {
int Level { get; }
}
Now all classes define that level-property:
clas DeathStar : IObject
{
public int Level { get { return this.reference_number; } }
}
clas Sith : IObject
{
public int Level { get { return this.anger_level; } }
}
clas StormTrooper: IObject
{
public int Level { get { return this.id; } }
}
Than you can use a constraint on your type T to implement that interface:
public static T CompareData<T>(List<T> list, T itemToFind) where T: IObject
Why not like this:
public static T CompareData<T>(List<T> list, Func<T, bool> predicate)
{
return database_list.FirstOrDefault(predicate);
}
And then use it like this:
var itemToFind = new ItemToFind();
var myObjectList = new List<MyObject>();
var item = CompareData<MyObject>(myObjectList, x=> x.MyObjectProperty == itemToFind.Id);
You could add a property selector:
public static class CompareService
{
public static T CompareData<T>(this List<T> list, T itemToFind, Func<T, int> propSelector)
{
int propToFind = propSelector(itemToFind); // cache
return database_list.FirstOrDefault(x => propSelector(x) == propToFind);
}
}
And call it like that:
listOfDeathstars.CompareData(deathStarToFind, ds => ds.reference_number);
listOfStormtroopers.CompareData(trooperToFind, t => t.id);
listOfSiths.CompareData(sithStarToFind, sith => new { sith.id, sith.anger_level});
Note: I added the this keyword in the signature to make it an extension (not sure if you intended that but forgot the keyword). And Where(predicate).FirstOrDefault() can be reduced to FirstOrDefault(predicate).

C# List the class property in lambda expression

I am trying to create an extension method to list the properties in the lambda expression.
Let say there is a Class named Example
public class Example {
Public string Name {get;set;}
Public string Description {get;set;}
}
the extension method can be something like below
public static void GetProperties<T>(this T obj) where T : new()
{
}
Expected usage : this.GetProperties<Example>(m=>m.
so when i type m=>m. should display both the properties (Name,Description).
I think you need to used Func:
public static void GetProperties<T, V>(this T obj, Func<T, V selector) where T : new()
{
}
Usage:
Example ex = new Example();
ex.GetProperties(m => m.Name); // Func<Example, string>
ex.GetProperties(m => m.Description); // Func<Example, string>
I really didn't understand the expected behavior inside the method. But you mentioned m.Name and m.Description. So a property selector is your way.
Func<Example, string> is a function that accepts an Example input parameter and returns a string (which is the property in case of Name and Description).
public static class PropertyUtility
{
public static string GetPropertyName<T>(this T entity, Expression<Func<T, object>> exp)
{
if (exp.Body is MemberExpression) {
return ((MemberExpression)exp.Body).Member.Name;
}
else {
var op = ((UnaryExpression)exp.Body).Operand;
return ((MemberExpression)op).Member.Name;
}
}
}
And use like this:
Example ex = new Example();
var property = ex.GetPropertyName(x => x.Description);

Implement extension Method C#

I need to implement this method :Reflectable reflect<T>(IEnumerable<T> src)
but i'm having troubles to get the expected output.
Hope someone can help me thanks.
Here the interface Reflectable:
interface Reflectable : IEnumerable<string> { Reflectable Prop(string propName);}
and the expected Output:
IEnumerable<Student> stds = //Students
IEnumerable<String> r1 = reflect(stds).Prop("Id");
IEnumerable<String> r2 = reflect(stds).Prop("Name").Prop("Id").Prop("Age");
IEnumerable<String> r3 = reflect(stds);
r1.ToList().ForEach(Console.Write); // {3213}{3124}{35454}...
r2.ToList().ForEach(Console.Write); // {Jose, 3213, 89}{Maria, 3124, 34}{Prominencia, 35454, 23}...
r3.ToList().ForEach(Console.Write); // {}{}{}...
I still think this idea of yours should be murdered in its sleep, or before birth...
public class ReflectionHelper<T>
: IEnumerable<string>
{
private string[] _properties;
private IEnumerable<T> _list;
public ReflectionHelper(IEnumerable<T> list, string[] properties)
{
_properties = properties;
_list = list;
}
public ReflectionHelper<T> Prop(string property)
{
return new ReflectionHelper<T>(_list, _properties.Concat(new string[]{ property}).ToArray());
}
public ReflectionHelper<T> Prop(string property)
{
return new ReflectionHelper<T>(_list, _properties.Concat(new string[] { property }).ToArray());
}
public static implicit operator List<string>(ReflectionHelper<T> helper)
{
return helper._list.Select(item => string.Join(",",
(from p in helper._properties
select typeof(T).GetProperty(p).GetValue(item, null)).ToArray())).ToList();
}
public IEnumerator<string> GetEnumerator()
{
return _list.Select(item => string.Join(",",
(from p in _properties
select typeof (T).GetProperty(p).GetValue(item, null)).ToArray()))
.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public static class ReflectionHelperExtension
{
public static ReflectionHelper<T> Prop<T>(this IEnumerable<T> items, string property)
{
return new ReflectionHelper<T>(items, new string[] { property });
}
}
If I understand what you're achieving, extension methods would be a way to do it, and here is how:
// This class has to be static for extension methods to be detected
public static class MyExtensions
{
// using "this" before the first parameter will make it an extension of the type
public static IEnumerable<string> Prop<T>(this IEnumerable<T> enumerable, string propName)
{
var type = typeof(T);
// Note that this can throw an exception if the property is not found
var info = type.GetProperty(propName);
// Here are your students, considering that <T> is <Student>
foreach (var item in enumerable)
{
// return the value fromt the item, I'm using ToString here for
// simplicity, but since you don't know the property type in
// advance, you can't really do anything else than assumne its
// type is plain "object"
yield return info.GetValue(item).ToString();
}
}
}
One thing you won't be able to do is to chain them like #lomed said, since it returns an IEnumerable.
public static class EnumerableReflect
{
private static string OneElementReflect<T>(T e, PropertyInfo[] props)
{
var values = props.Select(x => x.GetValue(e).ToString());
return "{" + string.Join(", ", values)) + "}";
}
public static IEnumerable<string> enumerbleString<T>(this IEnumerable<T> collection, params string[] PropertysNames)
{
var props = PropertysNames.Select(x => typeof(T).GetProperty(x)).ToArray();
return collection.Select(x => OneElementReflect(x, props));
}
}

Returning generic parameter with yield return

Hopefully this isn't a dupe, couldn't find anything related online
I'm getting a strange compile time error in the following extension method:
public static TCol AddRange<TCol, TItem>(this TCol e, IEnumerable<TItem> values)
where TCol: IEnumerable<TItem>
{
foreach (var cur in e)
{
yield return cur;
}
foreach (var cur in values)
{
yield return cur;
}
}
Error:
The body of 'TestBed.EnumerableExtensions.AddRange(TCol, System.Collections.Generic.IEnumerable)' cannot be an iterator block because 'TCol' is not an iterator interface type
Does this mean that generic constraints are not considered by the compiler when determining if a method qualifies for yield return use?
I use this extension method in a class which defines the collection using a generic parameter. Something like (in addition to a few type cast operators):
public class TestEnum<TCol, TItem>
where TCol : class, ICollection<TItem>, new()
{
TCol _values = default(TCol);
public TestEnum(IEnumerable<TItem> values)
{
_values = (TCol)(new TCol()).AddRange(values);
}
public TestEnum(params TItem[] values) : this(values.AsEnumerable()) { }
...
}
And in turn, used like (remember I have type cast operators defined):
TestEnum<List<string>, string> col = new List<string>() { "Hello", "World" };
string someString = col;
Console.WriteLine(someString);
Originally, my extension method looked like:
public static IEnumerable<TItem> AddRange<TItem>(this IEnumerable<TItem> e, IEnumerable<TItem> values)
{
...
}
Which compiles but results in:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type '<AddRange>d__61[System.String]' to type 'System.Collections.Generic.List1[System.String]'.
Is there an alternative way to do this?
As requested, here's a small sample:
class Program
{
public static void Main()
{
TestEnum<List<string>, string> col = new List<string>() { "Hello", "World" };
string someString = col;
Console.WriteLine(someString);
}
}
public class TestEnum<TCol, TItem>
where TCol : class, ICollection<TItem>, new()
{
TCol _values = default(TCol);
public TestEnum(IEnumerable<TItem> values)
{
_values = (TCol)(new TCol()).AddRange(values);
}
public TestEnum(params TItem[] values) : this(values.AsEnumerable()) { }
public static implicit operator TItem(TestEnum<TCol, TItem> item)
{
return item._values.FirstOrDefault();
}
public static implicit operator TestEnum<TCol, TItem>(TCol values)
{
return new TestEnum<TCol, TItem>(values);
}
}
public static class EnumerableExtensions
{
public static IEnumerable<TItem> AddRange<TItem>(this IEnumerable<TItem> e, IEnumerable<TItem> values)
{
foreach (var cur in e)
{
yield return cur;
}
foreach (var cur in values)
{
yield return cur;
}
}
}
To repro the compile-time exception:
class Program
{
public static void Main()
{
TestEnum<List<string>, string> col = new List<string>() { "Hello", "World" };
string someString = col;
Console.WriteLine(someString);
}
}
public class TestEnum<TCol, TItem>
where TCol : class, ICollection<TItem>, new()
{
TCol _values = default(TCol);
public TestEnum(IEnumerable<TItem> values)
{
_values = (TCol)(new TCol()).AddRange(values);
}
public TestEnum(params TItem[] values) : this(values.AsEnumerable()) { }
public static implicit operator TItem(TestEnum<TCol, TItem> item)
{
return item._values.FirstOrDefault();
}
public static implicit operator TestEnum<TCol, TItem>(TCol values)
{
return new TestEnum<TCol, TItem>(values);
}
}
public static class EnumerableExtensions
{
public static TCol AddRange<TCol, TItem>(this TCol e, IEnumerable<TItem> values)
where TCol : IEnumerable<TItem>
{
foreach (var cur in e)
{
yield return cur;
}
foreach (var cur in values)
{
yield return cur;
}
}
}
Let's simplify:
static T M() where T : IEnumerable<int>
{
yield return 1;
}
Why is this illegal?
For the same reason that this is illegal:
static List<int> M()
{
yield return 1;
}
The compiler only knows how to turn M into a method that returns an IEnumerable<something>. It doesn't know how to turn M into a method that returns anything else.
Your generic type parameter T could be List<int> or any of infinitely many other types that implement IEnumerable<T>. The C# compiler doesn't know how to rewrite the method into one that returns a type it knows nothing about.
Now, regarding your method: what is the function of TCol in the first place? Why not just say:
public static IEnumerable<TItem> AddRange<TItem>(
this IEnumerable<TItem> s1, IEnumerable<TItem> s2)
{
foreach(TItem item in s1) yield return item;
foreach(TItem item in s2) yield return item;
}
Incidentally, this method already exists; it is called "Concat".
I'm not sure what are you trying to accomplish, your method certainly doesn't look like AddRange(), because it doesn't add anything to any collection.
But if you write an iterator block, it will return an IEnumerable<T> (or IEnumerator<T>). The actual run-time type it returns is compiler generated and there is no way to force it to return some specific collection, like List<T>.
From your example, AddRange() simply doesn't return List<T>, which is why you can't cast the result to that type. And there is no way to make iterator block return List<T>.
If you want to create a method that adds something to a collection, it probably means you need to call Add(), not return some other collection from the method:
public static void AddRange<T>(
this ICollection<T> collection, IEnumerable<T> items)
{
foreach (var item in items)
collection.Add(item);
}
In response to your comment to Eric Lippert's answer:
I hoped for a solution that would work against IEnumerable but will settle with one for ICollection.
public static void AddRange<TCol, TItem>(this TCol collection, IEnumerable<TItem> range)
where TCol : ICollection<TItem>
{
var list = collection as List<TItem>;
if (list != null)
{
list.AddRange(range);
return;
}
foreach (var item in range)
collection.Add(item);
}
I defined the method as void to mimic the semantics of List<T>.AddRange().

How to pass anonymous property via lambda to a generic function?

I want to pass anonymous property using lambda to a generic function and access it there.
And how Do I access the property inside.
using (CommentsRepository commentsRepository = new CommentsRepository())
{
var comments = commentsRepository.GetAllComments();
Foo<Comment>(comments, 0,com=>com.ID); //Comment is an EF entity
}
public static void Foo<TObject>(IEnumerable<TObject> list, int iCurID, <Func<TObject, TProperty> property) where TObject : class
{
foreach (var cat in list.Where(/*How to access the property*/==iCurID)
{
int x = cat.property;
}
}
You just call the delegate:
public static void Foo<TObject>
(IEnumerable<TObject> list,
int iCurID,
Func<TObject, int> propertySelector) where TObject : class
{
foreach (var cat in list.Where(x => propertySelector(x) == iCurID))
{
}
}
Note that I had to change the type of the delegate to Func<TObject, int> as otherwise you couldn't compare it with iCurID.

Categories