[英]C# How to cast from Type without IConvertible using reflection
My returning value of the method CastTo is from the wrong type. 我的方法CastTo的返回值来自错误的类型。 The method CastTo has a returning parameter < T >.
CastTo方法有一个返回参数<T>。
The commented line is what it does without using reflection 注释行是它在不使用反射的情况下所做的
//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 is from the type Field but should be from the type FieldChoice. Choiceproduct来自Field类型,但应该来自FieldChoice类型。
The problem is, i can't create an instance of Fieldchoice before the method cause sharepoint just doesn't have a fittable constructor to allow creating it and i can't cast it to the Type with using Convert.changeType cause it doen't have a IConvertible implementation. 问题是,我无法在方法之前创建Fieldchoice的实例,因为sharepoint只是没有一个适合的构造函数来允许创建它,我无法使用Convert.changeType将它强制转换为Type,因为它不会有一个IConvertible实现。
Is there any other way i can cast my variable or change the return type of the method? 有没有其他方法可以转换我的变量或更改方法的返回类型?
我认为你的问题是你将field对象传递给genericCastTo而不是FieldChoice 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.