简体   繁体   中英

C# reflection, invoke method with different parameter types

I'm looking to invoke a static method with reflection that takes a Project and 3 strings as parameters but can't find a way to achieve this. Take the below as an example

internal class PropertyActionProcessor
{
    public static async Task<Solution> ModifyNameInDto(Project project, string parentName, string oldValue, string newValue)
    {
        return new AdhocWorkspace().CurrentSolution;
    }
}

If the method just took 3 strings I could successfully invoke the "ModifyNameInDto" with the line below.

solution = await (Task<Solution>)typeof(PropertyActionProcessor).GetMethod($"Modify{propertyName}InDto").Invoke(null, new[] { parentName, itemName, newValue });

However I need to pass it a Project as well but am getting the error "no best type found for implicitly typed array". There are no overloads that can help and I can't find a solution online, is this possible and if so how could I go about it?

For anyone unfamiliar with the Project object, the same error occurs when trying to pass an int instead.

You're getting the error "no best type found for implicitly typed array" because the parameters you're passing in have different types. If they were all strings, it is implicit that it is a string array. However in your case, as there are multiple different types, you need to give it a hint.

For example - if you provide new object[] { new Project(), parentName, itemName, newValue } , this should avoid the compile error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM