jeudi 23 septembre 2021

C# HashSet Overlap Reflection

I have two HashSets that I'm trying to use the Overlap method with via reflection. Here's what I've got so far in LinqPad. You'll see that invocation works and returns true. I'm expecting the same to happen with comp.

var bigSet = Enumerable.Range(1, 10).ToList().ToHashSet();
var smallSet = Enumerable.Range(3, 1).ToList().ToHashSet();
var p1 = Expression.Parameter(typeof(HashSet<int>));
var p2 = Expression.Parameter(typeof(HashSet<int>));

MethodInfo overlap = typeof(HashSet<int>).GetMethod("Overlaps");
var body = Expression.Call(overlap, p1, p2);
var lam = Expression.Lambda<Func<HashSet<int>, HashSet<int>>>(body, p1, p2);


var invocationTest = overlap.Invoke(bigSet, new object[] { smallSet });
Console.WriteLine(invocationTest);


var comp = lam.Compile()
var x = comp(bigSet, smallSet);

My problem is that comp has an off by one error in the arguments. I think I've got lam written incorrectly or possibly body as well and thats actually causing it. Thank you





Aucun commentaire:

Enregistrer un commentaire