|
Although as a programmer this approach appears to make sense to you (you are loading source code into a dynamic method), to the computer the approach does not make sense as the source code you are loading is not compiled. In order to achieve this, you would have to load the source code then compile it at run-time and then execute it most likely using reflection ;->
|
|
|
|
|
Those were my thoughts exactly, that I was stuck with reflection. Though, the context is a problem... I cannot fit the code in the context that I want.
Anyways, thank you for the replies.
Gonçalo A.
|
|
|
|
|
Ahhhh, now I understand. Do you have control over the code in the file, or is it a third party thing that you don't want to edit? If you have control, why not make the code a class with a method that does the work for you? For example, if you want to take in a string a return a boolean, make a static class "Container" with the method "public static bool DoWork(string input)". Does this help at all? I think the problem is you are trying to modify the running dll at run-time, which sounds like it is not possible (without hacked code and a lot of work). You need to branch to new code, execute it, then return, which is exactly what functions are for. Hope this helps,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Yes, I kind of wanted something very weird, I said it. But it's only logical that it's not possible. That would mean to alter a dll in runtime... The decision I came up to go around this is using reflection. I send in a sort of a sandbox with variables that I might want to alter in the code, fetch the code from the file, encapsulate it inside a neat namespace with a class and a method, compile it, and run it. Seems to do the trick. I had to create a few "syntax hacks" to access the sandbox, like...
<br />
$var as Label.Text = "something";<br />
and in runtime replace the "pretty" line into something more ugly to access the sandbox. It works, and does the job very well.
I also needed to create two more "syntax hacks" in case I need an extra reference, or to use an extra namespace that is not included by default in the assembly chunk.
<br />
#assembly "System.Drawing.dll";<br />
#using System.IO;<br />
Those three "hacks" are not very elegant, but... it allows me to have this done in a very generic way, which, in the current project, is the only way.
Thank you all for the replies... and... if anyone as any thoughts on this, please let me know.
Gonçalo A.
|
|
|
|
|
Hello.
I am new to c#.
When I am coding like this (I dont know whether it is right or wrong)
if (this.ColorGrade ="31-1" || this.ColorGrade="31-2" || this.ColorGrade="31-3")
{this.QGrd='F';}
I m getting the subject message.
What should I do?
|
|
|
|
|
Use == for comparision:
if (this.ColorGrade =="31-1" || this.ColorGrade=="31-2" || this.ColorGrade=="31-3")
this.QGrd='F';
As you can se you dont ha to include { and } when the if-line is followed by one statement only.
|
|
|
|
|
Viktor Nilsson wrote: As you can se you dont ha to include { and } when the if-line is followed by one statement only.
You're correct. Now, what does it have to do with the question that was asked?
only two letters away from being an asset
|
|
|
|
|
It is the
"if isn't needed then drop it"
school.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Viktor Nilsson wrote: As you can se you dont ha to include { and } when the if-line is followed by one statement only.
That is a matter of programming style. For readability, I always include the brackets:
if (this.ColorGrade == "31-1" || this.ColorGrade == "31-2" || this.ColorGrade == "31-3") {
this.QGrd='F';
}
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Guffa wrote: For readability, I always include the brackets:
Ditto me. Mind you, I hate the use of this.ColorGrade in the test. I'm not that enamoured of using this. . Note - I know this wasn't your code, so I'm not having a go here, merely stating my dislike.
|
|
|
|
|
bring back hungarian notation!!!
|
|
|
|
|
I'd never been a fan till i worked on a a java project recently where this. and super. had been used religiously throughout the project. After a while i started to quite like being able to instantly know where in the inheritance heirarchy something had come from.
Saying that I still leave them out in my own projects, just too lazy i suppose.
Russell
|
|
|
|
|
Jugnu wrote: if (this.ColorGrade ="31-1" || this.ColorGrade="31-2" || this.ColorGrade="31-3")
{this.QGrd='F';}
|| is a boolean condition, which means that it is expecting boolean tests around it. So, you need a boolean condition. Now in this if statement, the boolean condition is going to be == rather than = because = is an assignment statement and not a boolean condition. I hope that's clear enough.
|
|
|
|
|
I've written a plug-in to work with a 3rd party app.
The code has to be installed to 3rdPartyPath\Plugins so I need to override the install path choice for the user. I can delete the page where the user gets to choose an install path but I can't work out how to tell the app to install to my custom location. I seem to be able to call out to my own dlls which should be able to tell me where to install but i can't work out how to do anything with the return value.
Does anyone know how to do this?
Cheers,
Russell
|
|
|
|
|
I would say look into this[^] MSDN site. This website was discovered by looking through links on this[^] message thread. Hope these help,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Hey guys, ive ran into a little problem here.. im interfacing with an unmanaged c++ dll in my c# application.
the dll structure is as follows:
APIENTRY ReadImage(
hInstance hObject,
unsigned char* pData,
unsigned long dataSize);
with pData being the top address of the image reading buffer allocated by the calling application.
how would i declare a variable in c#, allocate a specific ammount of memory to it, then return the
memory address of said variable. this is got me stumped :/
|
|
|
|
|
Take a look at System.Runtime.InteropServices.Marshal and associated information
|
|
|
|
|
Thanks for the info mike, i took a look at the class but i still have no idea how to pass the memory address as a parameter to the dll.
|
|
|
|
|
|
Off the top of my head, I would expect you to pass in a StringBuilder object as pData.
|
|
|
|
|
Pete O'Hanlon wrote: I would expect you to pass in a StringBuilder object as pData.
Given the name of the function is ReadImage I assumed it would not be text data and therefore StringBuilder would not work?
|
|
|
|
|
Hi,
assuming the native function expects to get a buffer of sufficient size, this is
how you should do it:
- include a line using System.Runtime.InteropServices; // DllImport
- declare the native function as
[DllImport("myLibrary.dll")]
private extern static int ReadImage(IntPtr hObject, IntPtr pData, ulong dataSize);
- allocate an array of sufficient size (type is byte, uint, whatever you like);
- use class GCHandle methods Alloc (GCHandleType.Pinned) and AddrOfPinnedObject to get
pointer you need (an IntPtr). Pinning prevents the GC to move the array object around
until it gets freed again.
- call the native function as if it were part of your class (the extern static declaration
makes that possible)
- use GCHandle.Free to undo the pinning
|
|
|
|
|
Hi there.
I have an USB Device whick have to small functions.
It should be possible if I write 'GET' to it, it should then return a integer value. And if I write 'RES' it should reset the Integer value.
The Device is connected to my Computer with a USB-Serial cable. I have installed a driver i got with the cable.
I have tried using LibUsbDotNet, but when i tries to open a connection to the device the program just crashes and i cant do anything.
Anyone who can tell how to communicate with my device?
|
|
|
|
|
You said it was connected using a USB Serial cable?? Is the device exposed on a COM port?? Like COM1, COM2, Com3, ... Try talking to it over the serial port instead of going through the headache of using the devices' drivers.
|
|
|
|
|
The device has a serial port, so i have connected it to an USB port using a USB-Serial cable.
|
|
|
|