jeudi 15 février 2018

Using reflection to assign value to class fields

I am calling multiple methods of a single web service. For every method, i need to create a unique request and have to pass site credentials and user credentials everytime which is a very repetitive task. Please find the code below.

ClientService.AddOrUpdateClientsRequest clientRequest = new ClientService.AddOrUpdateClientsRequest();
        // Create and fill credentials
        clientRequest.SourceCredentials = new ClientService.SourceCredentials();
        clientRequest.SourceCredentials.SourceName = sourcename;
        clientRequest.SourceCredentials.Password = password;
        clientRequest.SourceCredentials.SiteIDs = siteIDs;


        clientRequest.UserCredentials = new ClientService.UserCredentials();
        clientRequest.UserCredentials.Username = "Siteowner";
        clientRequest.UserCredentials.Password = "apitest1234";
        clientRequest.UserCredentials.SiteIDs = siteIDs;


 ClientService.AddOrUpdateClientsRequest updateClientRequest = new ClientService.AddOrUpdateClientsRequest();
            // Create and fill credentials
            updateClientRequest.SourceCredentials = new ClientService.SourceCredentials();
            updateClientRequest.SourceCredentials.SourceName = sourcename;
            updateClientRequest.SourceCredentials.Password = password;
            updateClientRequest.SourceCredentials.SiteIDs = siteIDs;


            updateClientRequest.UserCredentials = new ClientService.UserCredentials();
            updateClientRequest.UserCredentials.Username = "Siteowner";
            updateClientRequest.UserCredentials.Password = "apitest1234";
            updateClientRequest.UserCredentials.SiteIDs = siteIDs;

 ClientService.UploadClientDocumentRequest uploadRequest = new ClientService.UploadClientDocumentRequest();

// Assign credentials for UploadClientDocumentRequest object

How can i create a generic function to assign values to same credentials fields but different type of objects. I was looking at using reflection but didn't get anything online





Aucun commentaire:

Enregistrer un commentaire