Invoking Member of a class using Class Name and Me

2020-03-06 03:19发布

问题:

I am trying to invoke function of a class using Reflection (assuming that object initialization as no dependency on Function to be invoked) like this

/// <summary>
    /// Calls a static public method. 
    /// Assumes that the method returns a string
    /// </summary>
    /// <param name="assemblyName">name of the assembly containing the class in which the method lives.</param>
    /// <param name="namespaceName">namespace of the class.</param>
    /// <param name="typeName">name of the class in which the method lives.</param>
    /// <param name="methodName">name of the method itself.</param>
    /// <param name="stringParam">parameter passed to the method.</param>
    /// <returns>the string returned by the called method.</returns>
    /// 
    public static string InvokeStringMethod5(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam)
    {
        //This method was created incase Method has params with default values. If has params will return error and not find function
        //This method will choice and is the preffered method for invoking 

        // Get the Type for the class
        Type calledType = Type.GetType(String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName));
        String s = null;

        // Invoke the method itself. The string returned by the method winds up in s.
        // Note that stringParam is passed via the last parameter of InvokeMember, as an array of Objects.

        if (MethodHasParams(assemblyName, namespaceName, typeName, methodName))
        {
            s = (String)calledType.InvokeMember(
                        methodName,
                        BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
                        null,
                        null,
                        new Object[] { stringParam });
        }
        else
        {
            s = (String)calledType.InvokeMember(
            methodName,
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
            null,
            null,
            null);
        }

        // Return the string that was returned by the called method.
        return s;

    }

    public static bool MethodHasParams(string assemblyName, string namespaceName, string typeName, string methodName)
    {
        bool HasParams;
        string name = String.Format("{0}.{1},{2}", namespaceName, typeName, assemblyName);
        Type calledType = Type.GetType(name);
        MethodInfo methodInfo = calledType.GetMethod(methodName);
        ParameterInfo[] parameters = methodInfo.GetParameters();

        if (parameters.Length > 0)
        {
            HasParams = true;
        }
        else
        {
            HasParams = false;
        }

        return HasParams;

    }  

This was taken form here.

Is there any other/better way to do this ??

Can this activity be generalship. Like using Dynamic and can call Non-Static methods in .Net 4.0 so that the return type can be independent.

I have never used dynamic keyword in actual scenario (only read some examples) to actul usage still unknown to me

Any help/direction in this regard would be appreciated Thanks

回答1:

To answer you query on dynamic; no, that wouldn't be a good fit here. dynamic is useful when the member-name (or operation) is known at compile-time, but is not provable to exist - basically, duck-typing. For example:

dynamic foo = GetSomeRandomObject();
foo.ThisShouldExist("abc");

this does similar things, but the usage is different. So yes, you're left with reflection. What you are doing is pretty-much right. The only thing I might change would be to obtain the MethodInfo, and work from there - although if you can change the API to accept a single string assemblyQualifiedName it would be more convenient and flexible. But perhaps:

public static string InvokeStringMethod5(string assemblyName,
    string namespaceName, string typeName, string methodName, string stringParam)
{
    string assemblyQualifiedName = string.Format("{0}.{1},{2}",
        namespaceName, typeName, assemblyName);
    Type calledType = Type.GetType(assemblyQualifiedName);
    if(calledType == null) throw new InvalidOperationException(
        assemblyQualifiedName + " not found");
    MethodInfo method = calledType.GetMethod(methodName,
        BindingFlags.Public | BindingFlags.Static);
    switch (method.GetParameters().Length)
    {
        case 0:
            return (string)method.Invoke(null, null);
        case 1:
            return (string)method.Invoke(null, new object[] { stringParam });
        default:
            throw new NotSupportedException(methodName
                + " must have 0 or 1 parameter only");
    }
}


回答2:

To answer the question on how to cast the result to a generic return type, the method would look something like:

public static T InvokeMethod<T>(string assemblyName, string namespaceName, string typeName, string methodName, string stringParam)
{
    // instead of String s = null;
    T methodResult = default(T);

    // instead of s = (String)calledType.InvokeMember(...)
    methodResult = (T)calledType.InvokeMember(...);

    // return s;
    return methodResult;
}