C
C#6mo ago
_populous_

✅ How can i turn this into a query?

Current code: foreach (var type in module.Types) { if (type.Name == "foo") { foreach (var method in type.Methods) { if (method.Name == "bar") MethodDefinition myMethod = method; } } } I am trying to turn this code into a query, so it's shorter and prettier.. but i can't seem to get it right.. MethodDefinition myMethod = from type module.Types where type.Name == "foo" ... I just get errors with whatever i try..
10 Replies
kurumi
kurumi6mo ago
module.Types
.Where(t => t.Name == "foo")
.SelectMany(t => t.Methods)
.Where(m => m.Name == "bar")
module.Types
.Where(t => t.Name == "foo")
.SelectMany(t => t.Methods)
.Where(m => m.Name == "bar")
something like this I guess
_populous_
_populous_OP6mo ago
Where do i put the assignment of: MethodDefinition myMethod = method ?
kurumi
kurumi6mo ago
what is this for?
_populous_
_populous_OP6mo ago
I need to get the myMethod reference, so i can pass it as an argument to a different function it's from mono.cecil library using it to inject .net code into a .net binary
kurumi
kurumi6mo ago
dont know much about this mono lib, but if you need all them to pass, use
var methods = module.Types
.Where(t => t.Name == "foo")
.SelectMany(t => t.Methods)
.Where(m => m.Name == "bar");

foreach(var method in methods)
{
DifferentFunction(method); // ?
method.Invoke(); // ?
}
var methods = module.Types
.Where(t => t.Name == "foo")
.SelectMany(t => t.Methods)
.Where(m => m.Name == "bar");

foreach(var method in methods)
{
DifferentFunction(method); // ?
method.Invoke(); // ?
}
_populous_
_populous_OP6mo ago
Oh i thought it would select only one.. ok i think i understand my mistake Yes that looks very good mono lib is amazing btw
kurumi
kurumi6mo ago
if you need only one, then use .FirstOrDefault()
_populous_
_populous_OP6mo ago
That's perfect man, thanks a lot.
kurumi
kurumi6mo ago
$close
MODiX
MODiX6mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server