如何创建接受不同参数类型的通用函数

时间:2016-05-30 09:46:02

标签: c# asp.net c#-4.0 servicenow

考虑以下代码:

<?php

require_once('.config.inc.php');

$sercretAccessKey = "XXXXX";
$site = $_GET['site'];
$serviceUrl = "";
$signatureUrl = "";
//UK marketplace
$marketplaceIdNumber = "A1F83G8C2ARO7P";
date_default_timezone_set('America/Phoenix');
$serviceUrl = "https://mws.amazonservices.de/Orders/2013-09-01";
$signatureUrl = "mws.amazonservices.de";

// Get total from 31 days ago
$t1 = date("c", time()-31*24*60*60);

$AccessKey = "AWSAccessKeyId=" . urlencode("xxxxx");
$action = "&Action=" . urlencode("ListOrders");
$fulfillmentChannel = "&FulfillmentChannel.Channel.1=" . urlencode("MFN");
$updateAfter = "&LastUpdatedAfter=" . urlencode($t1);
$marketplaceId = "&MarketplaceId.Id.1=" . urlencode($marketplaceIdNumber);
$orderStatus1 = "&OrderStatus.Status.1=" . urlencode("Unshipped");
$orderStatus2 = "&OrderStatus.Status.2=" . urlencode("PartiallyShipped");
$sellerID = "&SellerId=" . urlencode("xxxx");
$signatureMethod = "&SignatureMethod=" . urlencode("HmacSHA256");
$signatureVersion = "&SignatureVersion=" . urlencode("2");
$timeStamp = "&Timestamp=" . urlencode(date("c"));
$version = "&Version=" . urlencode("2013-09-01");
$stringToSignature = 
"POST\n" . 
"$signatureUrl\n" .
"/Orders/2013-09-01\n" .
$AccessKey . $action . $fulfillmentChannel . $updateAfter . $marketplaceId . $orderStatus1 . $orderStatus2 . $sellerID . $signatureMethod . $signatureVersion . $timeStamp . $version;

$signatureCode = hash_hmac('sha256', $stringToSignature, $sercretAccessKey, true);

$signatureCodeBaseEncoded = base64_encode($signatureCode);
$signatureCodeEncoded = urlencode($signatureCodeBaseEncoded);
$signature = "&Signature=" . $signatureCodeEncoded;

$targetURL = $serviceUrl . "?" . $AccessKey . $action . $fulfillmentChannel . $updateAfter . $marketplaceId . $orderStatus1 . $orderStatus2 . $sellerID . $signatureMethod . $signatureVersion . $timeStamp . $version . $signature;

// create context
$context = array('header' => 'Content-Type: text/xml');

$ci = curl_init($targetURL);
curl_setopt($ci, CURLOPT_POST, 1);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_POSTFIELDS,"xmlRequest");

$result = curl_exec($ci);
echo $result;
?>

这段代码多次重复10次。因此,我决定使用一个通用函数,在传递不同类型时接受上面的代码。

我需要将 Url Credentials 作为新创建函数的响应。

作为参考,SomeServiceType是ServiceNowType, UserName和Password是String类型。

我试过

var list = new SomeServiceType
{
    Url = "htp://www.test.com/",
    Credentials = new NetworkCredential(UserName, Password)
};

url的类型为string,NetworkCredential的类型为c#class

2 个答案:

答案 0 :(得分:2)

您可以使用反射和激活器来创建方法,如下所示

 public T MyMethod<T>(string url, string userName, string userPassword) {
       var myService = Activator.CreateInstance(typeof(T));
      System.Reflection.PropertyInfo URL = myService.GetType().GetProperty("Url");
      System.Reflection.PropertyInfo NetworkCred = myService.GetType().GetProperty("Credentials");
      URL.SetValue(myService, url, null);
      NetworkCred.SetValue(myService, new System.Net.NetworkCredential(userName, userPassword), null);
      return (T)myService;
   }

呼叫为

var list =  MyMethod<SomeServiceType>(url,userName,userPassword);

答案 1 :(得分:0)

你可以在asp.net中做到这些。

        public static List<SomeServiceType> MyFunction(string url, string UserName, string Password)
        {
            List<SomeServiceType> list = new List<SomeServiceType>();
            if (url != null && UserName != null && Password != null)
            {
                list = new SomeServiceType()
                {
                    Url = "htp://www.test.com/",
                    Credentials = new NetworkCredential(UserName, Password)
                };
            }
            return list;
        }