10-15-2016, 02:50 PM | #1 |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Reflection.Emit
Give me your thoughts on it please. I'm currently studying it on my own for a project of mine and just wanted to hear more about what people think of it.
OpCodes is giving me some trouble since there is so many damn options and I'm not so good at memory management and such low-level skills. Also, I've heard Expression.Compile is great too but not as versatile and it's limited by lambda usage. Yes/no ? In what context to pick one or the other ? Thanks |
10-15-2016, 05:39 PM | #2 |
Dark Chancellor
|
Re: Reflection.Emit
are there more versatile or useful classes you can use for the same function or is this as good as it gets
__________________
Last edited by kommisar; 10-15-2016 at 05:41 PM.. |
10-15-2016, 06:04 PM | #3 |
longing
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
|
Re: Reflection.Emit
Wow I was writing a post and my ipad deleted it before I submitted :(
I'm not too familiar with it in c#, but reflection is pretty useful in other languages.. Basically, it allows for you to dynamically create functions, objects etc, and create them or change them during execution, and the program will be able to interact with them as though they were native parts of the code. It also allows you to expose certain elements to external code. A pretty solid practical example of this is runescape bots - imagine rewriting the method that displays the game itself to also output data on objects that are appearing in the world, such as their coordinates. On the topic of expression compile.. I am predicting one benefit of not using it is because of how arbitrary it could be.. You wouldn't want someone to be able to arbitrarily add code, so perhaps using other methods allows for things such as ensuring it is valid, ensuring it comes from a legit source etc.. I'm reading more on this IS stuff.. It seems like the idea is to have code that is system and code independant, hence why you'd be writing op codes and manually managing the stack, that way it's irrelevant which language executes your IS packag or where it's run. Regarding the op code stuff, you are basically manually managing a stack.. For example if you want to print letters, you can't just call a print statement, as that would be platform dependant.. Instead, you need to manually place the characters on the stack, then pop them to the output buffer etc.. How familiar are you with data structures and basic assembly stuff (things like registers, subroutines, traps etc).. I'd recommend researching some assembly, how it works in the CPU etc... I'm imagine since you are trying reflection you are familiar with this stuff already though :p From what I can see, reflection.emit is just an abstraction of writing this op code stuff manually, essentially it converts to machine code anyways... Or maybe I'm just talking out my ass I know literally nothing ;D |
10-15-2016, 06:25 PM | #4 |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
Thanks for the reply!
Well I'm definitely thinking about writing some kind of utility class that will ensure the validity of the stack manipulations cuz I don't see any way to make sure you always use the right opcodes -.- I'm not sure how you'd use reflection (.emit at least) to manipulate runescape UI unless you actually have some kind of access to its source code or something (Idk runescape much lol), since that'd be cross-domain level and well there's most likely some security there. In any case I'm not that good at it yet. I was thinking about starting some program that'd basically take text (that you'd write on UI) and convert that into code, then run it live. Ofc I would only be implementing the basic structures such as simple types (primitives only first), no delegates, maybe a few simple events (??), etc. |
10-15-2016, 06:35 PM | #5 | ||
Carry your failures proud
Join Date: Sep 2015
Age: 23
Posts: 644
|
Re: Reflection.Emit
I think the main concern with Reflection.Emit is that it has a narrow use case.
If you need to write IL code for some reason, with the only use pretty much being a compiler or generator of some description, then Reflection.Emit is great, because it lowers the complexity required to write IL code dynamically. The other conceivable use case of writing IL code reflects the use of similar constructs in Java: modifying a third-party method at runtime. Unfortunately, C# makes this range from "you really, REALLY, shouldn't be doing this what so ever" to impossible, depending on the exact use case. A shame really. Expression.Compile seems to be (I probably should have mentioned I'm not qualified to speak definitively on this at all) a shortcut for writing IL should you already have C# expressions. Examples of when this would be the case are writing a C# compiler in C#, or when you are generating code based on some higher level parameters, and it'd be easier to just write it in C# expressions as compared to writing the IL yourself. In terms of Expression.Compile vs Reflection.Emit, there doesn't seem to be anything indicating to me that either one can generate something that is functionally different from the other, but Expression.Compile guarantees something that functions a certain way where as Reflection.Emit guarantees something that *is* a certain way, which if you're writing IL code for say, performance reasons, could be of interest.
__________________
Quote:
Quote:
|
||
10-15-2016, 06:38 PM | #6 |
longing
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
|
Re: Reflection.Emit
Op, if I'm correct from what I can see, this seems like its somewhat similar to what you want to do https://github.com/Ineedajob/RSBot/t...bot/loader/asm
It sounds like you are trying to write an interpreter for plaintext to code? Better start reading about abstract syntax trees :p The bots work because regardless of the source code or not, java runs on its own virtual machine which I'd imagine is being analyzed, unsure exactly how it works though :( Last edited by Dinglesberry; 10-15-2016 at 06:40 PM.. |
10-15-2016, 06:41 PM | #7 | |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
Quote:
I'm curious to see if you'd have any examples to support this quote ? If that's really the case then I might give it a try in Java since I have no problem using it. |
|
10-15-2016, 06:41 PM | #8 |
longing
Join Date: Dec 2007
Location: Ontario, Canada
Posts: 2,680
|
Re: Reflection.Emit
Might be interesting reading, seems like it is more general than about a specific language
https://ruslanspivak.com/lsbasi-part1/ |
10-15-2016, 06:49 PM | #9 | ||
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
Quote:
As for the git repo I might read that a little bit but I'm sticking with c# if there's a possibility I can get it done with that. :) Quote:
Last edited by xXOpkillerXx; 10-15-2016 at 06:55 PM.. |
||
10-15-2016, 09:21 PM | #10 | |||
Carry your failures proud
Join Date: Sep 2015
Age: 23
Posts: 644
|
Re: Reflection.Emit
Quote:
http://stackoverflow.com/questions/7...c-sharp-method Just by doing a bit of pointer work you can replace a "vanilla" method with another "vanilla" method, and there's a link there to really hack it up for other cases. Purportedly it works but it involves native level stuff, and only works on .NET. I couldn't follow it very well myself. On the other hand, Java lets you really dig into the internals of code loading, making dynamic modification portable and (relatively) easy. EDIT: It should be noted that both C# and Java suffer from a similar problem in this regard: A launch point. Any loaded code in both cases will not be updated. In C# this is a massive problem because there's no easy way to reload the code that's already loaded, or ensure that you can modify the code before it's loaded. In Java you have multiple options. One is to launch a victim program from another Java program, choosing your custom loading code along the way, or modify the victim program with a class that when loaded, all it does is reload the entire program using custom loading code on a new thread, and kills the current thread.
__________________
Quote:
Quote:
Last edited by Soundwave-; 10-15-2016 at 10:15 PM.. |
|||
10-15-2016, 10:23 PM | #11 |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
?? Not sure if I'm just lacking the knowledge here or not fully understanding the issue you're pointing out, but you can inject a Main(args) easily and run it. Please clarify/explain to me if I'm not getting what you mean. ^^
|
10-16-2016, 03:36 AM | #12 | ||
Carry your failures proud
Join Date: Sep 2015
Age: 23
Posts: 644
|
Re: Reflection.Emit
If you're modifying a third-party assembly then your code needs to be ran at some point, requiring that you either be entirely in control of the execution of the assembly or that your code has already been incorporated somehow.
__________________
Quote:
Quote:
|
||
10-16-2016, 07:30 AM | #13 |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
Oh yeah absolutely. But that's no flaw to me; if a third party code can be easily modified at runtime it's just a major security flaw if you ask me lmao. But yeah, good thing I only want to modify my own code with reflection. I'll spend some time on it today, I'll let you guys know if I get anything nice ^^
|
10-16-2016, 03:34 PM | #14 | |||
Carry your failures proud
Join Date: Sep 2015
Age: 23
Posts: 644
|
Re: Reflection.Emit
Quote:
__________________
Quote:
Quote:
|
|||
10-16-2016, 03:54 PM | #15 | |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
Quote:
|
|
10-16-2016, 04:39 PM | #16 | |||
Carry your failures proud
Join Date: Sep 2015
Age: 23
Posts: 644
|
Re: Reflection.Emit
Quote:
I am of the opinion that parsing code and using expressions will be much easier than any sort of disassembly/reassembly work, but I don't know what exactly you're trying to do so dunno.
__________________
Quote:
Quote:
|
|||
10-16-2016, 04:51 PM | #17 | |
✘ Forever OP✘
Join Date: Dec 2008
Location: Canada,Quebec
Age: 28
Posts: 4,171
|
Re: Reflection.Emit
Quote:
Tbh the final goal isn't so clear to me either, but I'm thinking about writing code in idk a textbox in the app, have it compile and run when I want to. Something like that. |
|
10-16-2016, 05:44 PM | #18 | |||
Carry your failures proud
Join Date: Sep 2015
Age: 23
Posts: 644
|
Re: Reflection.Emit
Quote:
I believe there's fairly rigorous lexical specifications for C# which definitely simplifies your job in that regard though.
__________________
Quote:
Quote:
|
|||
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|