0

I've looked through the other posts and have not found anything that specifically hits on this issue, so here we go...

I have an external DLL class...

namespace Utility
{
    //o--------------------------------o
    //| string style Enumerator class  |
    //o--------------------------------o
    public class StringEnumerator : IEnumerable<string>
    {
        protected List<string> _elements;

        //our constructor
        public StringEnumerator(string[] array)
        {
            this._elements = new List<string>(array);
        }

        IEnumerator<string> IEnumerable<string>.GetEnumerator()
        {
            return this._elements.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this._elements.GetEnumerator();
        }
    }
}

In my testing application I can get the constructor but I can't invoke it...

            Type tmpType2 = localCore.GetType("Utility.StringEnumerator", false, true);
            object ot2a = Activator.CreateInstance(tmpType2, null);
            // Prove that we an object.
            DisplayBuffer($"A {ot2a.GetType().Name} object has been created");
            foreach (MethodInfo mi in tmpType2.GetMethods())
            {
                DisplayBuffer($"  Utility.StringEnumerator.MethodInfo.Name: {mi.Name}");
            }
            Type[] conTypes = { typeof(string[]) };
            // Get a reference to the correct constructor.
            ConstructorInfo ci2 = tmpType2.GetConstructor(conTypes);
            DisplayBuffer($"Utility.StringEnumerator.ConstructorInfo.Name: {ci2.Name}");
            
            // Prepare the parameters.
            object[] args4 = { "Help", "me", "please" };

            // Invoke the constructor and assign the result to a variable. 
            object o4 = ci2.Invoke(args4); //<-- This causes an error
            DisplayBuffer($"Invoked: {o4}");

The error I get is: System.Reflection.TargetParameterCountException: 'Parameter count mismatch.'

If I try this...

object o4 = ci2.Invoke(new object[] { "test string to pass" });

I get the error: System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'System.String[]'.'

Thanks to Peter for the help... this was the solution:

string[] args4 = { "test", "array", "of", "strings" };
object o4 = ci2.Invoke(new object[] { args4 });
  • Given that there's no parameterless constructor, it's not clear to me how you get past `Activator.CreateInstance(tmpType2, null);`. Nor why you don't just use that method later. The whole implementation above seems suspect. That said, you are trying to invoke a constructor that has three arguments, which of course doesn't exist. You need to wrap the `args4` array in an `object[]`: `ci2.Invoke(new object[] { args4 });` – Peter Duniho Jul 02 '21 at 18:24
  • Hi Peter, when I try ci2.Invoke(new object[] { args4 }); I get 'Object of type 'System.Object[]' cannot be converted to type 'System.String[]'.' – GuildOfCalamity Jul 02 '21 at 18:29
  • Well, you asked for a constructor that has a `string[]` argument. Again, the entire block of code seems suspect to me, but it's not surprising you get that exception. You've declared `args4` as `object[]` and then tried to pass it to a method that requires `string[]`. Since the values are string values, why not just declare `args4` as `string[]`? – Peter Duniho Jul 02 '21 at 18:44
  • I also tried ci2.Invoke(new string[] { "test", "of", "strings" }); but that results in the error: 'Parameter count mismatch.' I have been able to call other methods from external static classes without issue, but this one bothers me for some reason. This is purely an academic exercise. – GuildOfCalamity Jul 02 '21 at 19:37
  • _"I also tried ci2.Invoke(new string[] { "test", "of", "strings" })"_ -- why didn't you try [what was suggested before](https://stackoverflow.com/questions/68229611/assembly-invoking-using-constructorinfo-with-reflection?noredirect=1#comment120586765_68229611)? – Peter Duniho Jul 02 '21 at 19:59
  • Peter, I did try that and got 'Object of type 'System.String' cannot be converted to type 'System.String[]' – GuildOfCalamity Jul 02 '21 at 20:28
  • You have to make _all_ of the fixes that have been explained to you. When the code has multiple things wrong with it, fixing only one thing or another isn't going to solve your problem. – Peter Duniho Jul 02 '21 at 20:48
  • OK, this worked... string[] args4 = { "test", "array", "of", "strings" }; object o4 = ci2.Invoke(new object[] { args4 }); – GuildOfCalamity Jul 02 '21 at 20:51
  • Your code has a lot of problems. But the two that relate to the statement you're asking about are: **you are failing to recognize that the argument that is passed to the `Invoke()` method needs to be _an array of arguments_, not the argument itself**, and **the type of the argument you need to pass _in the array_, is an array of strings, i.e. `string[]`, and not `object[]` as you've declared the `args4` variable**. Fix those two things and the code will work. – Peter Duniho Jul 02 '21 at 20:54
  • Thanks for you help Peter, I have updated the post. – GuildOfCalamity Jul 02 '21 at 20:57

0 Answers0