我有一个接收动态对象和类型为字符串的函数。 我想将对象转换为我的字符串中的类型。C# - 发送类型作为参数并返回基于它的类型
public void PostAutomaticRule(dynamic automaticRuleObject, string ruleType)
{
switch (ruleType)
{
case "Increase_budget":
ConvertToAutomaticRule(typeof(IncreaseBudgetRule), ref automaticRuleObject);
break;
}
}
private void ConvertToAutomaticRule<T>(Type type, ref dynamic ruleObject)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var json = serializer.Serialize(ruleObject);
var c = serializer.Deserialize<type>(json);
}
我想转换为等级:
public class IncreaseBudgetRule
{
public string automaticRuleName { get; set; }
public string givePrioity { get; set; }
}
我有很多的规则类型,所以我想这个函数接收类型和对象,并返回我送类型的对象在函数中。 我该怎么做到这一点?
ConvertToAutomaticRule有一个未使用的泛型参数。我认为那是你需要的。 –