CLR.ahk question

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

CLR.ahk question

11 May 2017, 19:53

Hey guys,

I meant to ask this awhile ago, and since forgot the specific code (sorry about that), but the question is pretty simple: How would you call static/class methods?

For example, let's say I want to use Convert.ToInt32 in my code (a dumb example, I know, cuz AHK handles stuff like this automagically :lol:). You don't create an instance of the Convert class, you just call ToInt32 on the class. In fact I'm pretty sure if your C# code contains "new Convert();" you'll get an error, because that's not how that class was meant to be used.

But the documentation on CLR.ahk didn't seem to address that scenario. I remember there was a CLR_CreateObject function (again, I probably didn't get that exactly right, sorry) which I understood to be equivalent to instantiating an object. Or am I wrong about that? Thanks.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: CLR.ahk question

12 May 2017, 03:17

There are two ways to call static methods:
  1. Use .NET code (e.g. compile some C#).
  2. Use Reflection via COM.
CLR.ahk itself uses #2. For example, CLR_LoadLibrary invokes the static methods Assembly.LoadWithPartialName and Assembly.LoadFrom.

The CLR.ahk documentation only addresses functionality that CLR.ahk provides. It does not provide any helpers for calling static methods; that must be done using the standard interfaces that .NET provides (via COM callable wrappers).

You are correct about CLR_CreateObject; it is equivalent to new, and therefore irrelevant.
Miguel7
Posts: 186
Joined: 08 Sep 2014, 07:06

Re: CLR.ahk question

12 May 2017, 12:27

Interesting. Guess I'll have to see how you did it in CLR.ahk itself. Thanks for the info. :)
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: CLR.ahk question

12 May 2017, 23:50

Code: Select all

myCode =
(
	using System;	
	using System.Windows.Forms;
	class myClass
	{
		public void myMethod()
		{
			var myCharacter = '가';
			MessageBox.Show( 
				myCharacter.ToString() +"\n"+ 
				Convert.ToInt32(myCharacter));
		}
	}
)
myReference = 
(	Join|
	System.dll
	System.Windows.Forms.dll
	mscorlib.dll
)
CLR_CreateObject(CLR_CompileC#(myCode, myReference), "myClass").myMethod()

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 274 guests