Can possible casting object from string-value to classType

3294 views c#
7

I made a code like this.

private void MakeRowUsingMethodName(string pMethodName,params object[] var)
    {

        DataTable dt = dataSet1.Tables[0];
        DataRow row = dt.NewRow();
        row = dt.NewRow();

        System.Reflection.MethodInfo pMethod = typeof(ShellSection).GetMethod(pMethodName);
        int paramIndex = 0;
        string paramType = string.Empty;
        string paramName = string.Empty;
        int paramCount = pMethod.GetParameters().Length;
        string MethodName = pMethod.Name;

        foreach (ParameterInfo pParameter in pMethod.GetParameters())
        {

            paramIndex = pParameter.Position;
            paramName = pParameter.Name;
            paramType = pParameter.ParameterType.Name;

            row[paramIndex] = var[paramIndex];

        }
        dt.Rows.Add(row);
    }

It's work fine now, but i want more flexibility use other class.

but this part is hard to me.

System.Reflection.MethodInfo pMethod = typeof(ShellSection).GetMethod(pMethodName);

that "ShellSection" is ClassName,

so i wondered about it can be change ClassName to string-value.

And i don't know this method convert to like global method.

If only this part can be modified, I can make more flexible code.

PS.

I'll fix my question.

string className = "ShellSection"; or other things

typeof("ShellSection").GetMethod(pMethodName); or

typeof(className).GetMethod(pMethodName);

I am asking about the possibility and the method of operation as above.

answered question

1 Answer

5

You can try to use Type.GetType method it supports you get Type by class string name.

Type.GetType(ShellSection).GetMethod(pMethodName);

instead of

typeof(ShellSection).GetMethod(pMethodName);

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories