简体   繁体   中英

C# How to cast from Type without IConvertible using reflection

My returning value of the method CastTo is from the wrong type. The method CastTo has a returning parameter < 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.

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.

Is there any other way i can cast my variable or change the return type of the method?

我认为你的问题是你将field对象传递给genericCastTo而不是FieldChoice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM