![](/img/trans.png)
[英]How toDetermine if an object can be cast to a different type, without IConvertible
[英]C# How to cast from Type without IConvertible using reflection
我的方法CastTo的返回值来自错误的类型。 CastTo方法有一个返回参数<T>。
注释行是它在不使用反射的情况下所做的
//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的实例,因为sharepoint只是没有一个适合的构造函数来允许创建它,我无法使用Convert.changeType将它强制转换为Type,因为它不会有一个IConvertible实现。
有没有其他方法可以转换我的变量或更改方法的返回类型?
我认为你的问题是你将field对象传递给genericCastTo而不是FieldChoice 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.