C#如何使用反射从没有IConvertible的Type进行强制转换

时间:2019-04-29 08:29:30

标签: c# sharepoint reflection invoke

我返回的方法CastTo的值来自错误的类型。 方法CastTo具有返回参数

注释行是不使用反射的作用

            //FieldChoice ChoiceProduct = clientContext_temp.CastTo<FieldChoice>(field);
            Type FieldChoiceType = sharepointClient.GetType("Microsoft.SharePoint.Client.FieldChoice");
            object FieldChoice = Activator.CreateInstance(FieldChoiceType, sharepointClient, web.);
            MethodInfo castToMethodInfo = typeclientContext.GetMethod("CastTo");
            MethodInfo genericCastTo = castToMethodInfo.MakeGenericMethod(field.GetType());
            var ChoiceProduct = genericCastTo.Invoke(clientContext, new object[] { field });
            ChoiceProduct = Convert.ChangeType(ChoiceProduct, FieldChoiceType);

Choiceproduct来自Field类型,但应来自FieldChoice类型。

问题是,在方法导致共享点没有合适的构造函数以允许创建它之前,我无法创建Fieldchoice的实例,并且我无法使用Convert.changeType将其转换为Type导致它没有IConvertible实现。

我还有其他方法可以强制转换变量或更改方法的返回类型吗?

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您正在将 field 对象传递给genericCastTo,而不是 FieldChoice

相关问题