/// <summary> /// Executes the proxy method using reflection. /// </summary> /// <param name="proxy">A GlymaRepositoryProxy object used to execute the method.</param> /// <param name="methodInstance">The method to execute.</param> /// <param name="methodArgs">The parameters of the method.</param> /// <param name="entityFields">A ITypeDescriptorCollection object that contains the fields to retrieve for the entity containing the method to execute.</param> protected virtual void ExecuteProxyMethod(GlymaRepositoryProxy proxy, IMethodInstance methodInstance, object[] methodArgs, ITypeDescriptorCollection entityFields) { // Extract the method details required to execute it using reflection. IMethod method = methodInstance.GetMethod(); string methodName = (method.LobName != null) ? method.LobName : method.Name; List <Type> parameterTypes = new List <Type>(); List <object> parameters = new List <object>(methodArgs.Length); IParameterCollection parameterDefinitions = method.GetParameters(); foreach (IParameter parameterDefinition in parameterDefinitions) { if (parameterDefinition.Direction != DirectionType.Return) { Type parameterType = parameterDefinition.TypeReflector.ResolveDotNetType(parameterDefinition.GetRootTypeDescriptor().TypeName, method.GetDataClass().GetLobSystem()); if (parameterDefinition.Direction != DirectionType.In) { parameterType = parameterType.MakeByRefType(); } parameters.Add(methodArgs[parameterDefinition.OrdinalNumber]); parameterTypes.Add(parameterType); } } // Pass the entity fields as the last parameter to the proxy method. parameters.Add(entityFields); parameterTypes.Add(typeof(ITypeDescriptorCollection)); // Execute the method using reflection. object[] parametersArray = parameters.ToArray(); MethodInfo proxyMethodInfo = typeof(GlymaRepositoryProxy).GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, parameterTypes.ToArray(), null); if (proxyMethodInfo == null) { throw new Microsoft.BusinessData.Runtime.RuntimeException("A method with the name \"" + methodName + "\" with the specified parameters could not be found in the proxy."); } object result = proxyMethodInfo.Invoke(proxy, parametersArray); // Set the parameter values using the results of the method execution. foreach (IParameter parameterDefinition in parameterDefinitions) { if (parameterDefinition.Direction == DirectionType.Return) { methodArgs[parameterDefinition.OrdinalNumber] = result; } else { methodArgs[parameterDefinition.OrdinalNumber] = parametersArray[parameterDefinition.OrdinalNumber]; } } }
protected void UpdateCustomer_Click(object sender, EventArgs e) { // Make sure we have values for the entity namespace and name. if (!EntityValuesAreSet) { DisplaySetPropertyValuePrompt(true); return; } else { DisplaySetPropertyValuePrompt(false); } // Do simple validation of the customer ID. Make sure it is // an integer. int customerID = -1; if (!ValidateCustomerID(CustomerID.Text, ref customerID)) { StatusLabel.ForeColor = Color.Red; StatusLabel.Text = "Please enter an integer for the Customer ID value."; return; } try { using (new Microsoft.SharePoint.SPServiceContextScope( SPServiceContext.GetContext(SPContext.Current.Site))) { // Get the BDC service and metadata catalog. BdcService service = SPFarm.Local.Services.GetValue <BdcService>(String.Empty); IMetadataCatalog catalog = service.GetDatabaseBackedMetadataCatalog( SPServiceContext.Current); // Get the entity using the specified name and namespace. IEntity entity = catalog.GetEntity(EntityNamespace, EntityName); ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value; // Create an Identity based on the specified Customer ID. Identity identity = new Identity(customerID); // Get a method instance for the Updater method. IMethodInstance method = entity.GetMethodInstance("UpdateCustomer", MethodInstanceType.Updater); // The UpdateCustomer method of the external content type // maps to the UpdateCustomer method in the AdventureWorks // web service. Looking at the source for the web service // shows that the UpdateCustomer method has the following // signature: // // public void UpdateCustomer(SalesCustomer customer) // // The SalesCustomer type has the following layout: // // public class SalesCustomer // { // public int CustomerId { get; set; } // public String Title { get; set; } // public String FirstName { get; set; } // public String MiddleName { get; set; } // public String LastName { get; set; } // public String EmailAddress { get; set; } // public String Phone { get; set; } // public DateTime ModifiedDate { get; set; } // } // Get the collection of parameters for the method. // In this case there is only one. IParameterCollection parameters = method.GetMethod().GetParameters(); // Use type reflection to get an instance of a // SalesCustomer object to pass as a parameter. ITypeReflector reflector = parameters[0].TypeReflector; ITypeDescriptor rootTypeDescriptor = parameters[0].GetRootTypeDescriptor(); object[] methodParamInstances = method.GetMethod().CreateDefaultParameterInstances( method); Object instance = methodParamInstances[0]; // Get type descriptors for each of the SalesCustomer // members. ITypeDescriptor customerIDTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[0]; ITypeDescriptor titleTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[1]; ITypeDescriptor firstNameTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[2]; ITypeDescriptor middleNameTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[3]; ITypeDescriptor lastNameTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[4]; ITypeDescriptor emailAddressTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[5]; ITypeDescriptor phoneTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[6]; ITypeDescriptor modifiedDateTypeDescriptor = rootTypeDescriptor.GetChildTypeDescriptors()[7]; // Set the values of the SalesCustomer object members // with the values specified by the user. reflector.Set(customerIDTypeDescriptor, rootTypeDescriptor, ref instance, customerID); reflector.Set(titleTypeDescriptor, rootTypeDescriptor, ref instance, Title.Text); reflector.Set(firstNameTypeDescriptor, rootTypeDescriptor, ref instance, FirstName.Text); reflector.Set(middleNameTypeDescriptor, rootTypeDescriptor, ref instance, MiddleName.Text); reflector.Set(lastNameTypeDescriptor, rootTypeDescriptor, ref instance, LastName.Text); reflector.Set(emailAddressTypeDescriptor, rootTypeDescriptor, ref instance, Email.Text); reflector.Set(phoneTypeDescriptor, rootTypeDescriptor, ref instance, Phone.Text); reflector.Set(modifiedDateTypeDescriptor, rootTypeDescriptor, ref instance, DateTime.Now); // Execute the updater method, passing the parameter. entity.Execute(method, LobSysteminstance, ref methodParamInstances); StatusLabel.ForeColor = Color.Green; StatusLabel.Text = "Customer successfully updated."; } } catch (Exception ex) { StatusLabel.ForeColor = Color.Red; StatusLabel.Text = "Unable to find customer with ID = " + CustomerID.Text + ". " + ex.Message; } }