From 08cb07d18af7010c30a4e15669257189bad29d4f Mon Sep 17 00:00:00 2001 From: Aleksander Olszewski <65036788+tickylolokaka@users.noreply.github.com> Date: Tue, 23 Aug 2022 12:01:29 +0200 Subject: [PATCH] Fixed InstantiateClass function There were 2 issues with the InstantiateClass function: - The parameters namespaceName and className didn't do anything - The classInstance wasn't returned --- src/first-steps/methods.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/first-steps/methods.md b/src/first-steps/methods.md index 2355c42..e82ad7d 100644 --- a/src/first-steps/methods.md +++ b/src/first-steps/methods.md @@ -27,7 +27,7 @@ In this case we'll be using the manual way of getting references to methods, but MonoObject* InstantiateClass(const char* namespaceName, const char* className) { // Get a reference to the class we want to instantiate - MonoClass* testingClass = GetClassInAssembly(s_AppAssembly, "", "CSharpTesting"); + MonoClass* testingClass = GetClassInAssembly(s_AppAssembly, namespaceName, className); // Allocate an instance of our class MonoObject* classInstance = mono_object_new(s_AppDomain, testingClass); @@ -39,6 +39,8 @@ MonoObject* InstantiateClass(const char* namespaceName, const char* className) // Call the parameterless (default) constructor mono_runtime_object_init(classInstance); + + return classInstance; } void CallPrintFloatVarMethod(MonoObject* objectInstance) @@ -147,4 +149,4 @@ In the second example we don't just declare a `void*`, but rather an *array* of If you're wondering how Mono knows what the size of the data array that we pass is, it's simply because it expects the total size of that array to equal the size of *all* the parameters in the C# method, meaning if the array size doesn't match the parameter count you will end up with problems, and Mono *may* not tell you about it. -And that's the basics of how we can retrieve and invoke C# methods from C++! I will obviously go into more depth about parameter types and how we can convert between C++ types and C# types later on. \ No newline at end of file +And that's the basics of how we can retrieve and invoke C# methods from C++! I will obviously go into more depth about parameter types and how we can convert between C++ types and C# types later on.