尝试在C#中使用带有Set&的静态类Property Extension的反射。获取房产价格

时间:2017-07-18 13:53:56

标签: c# json reflection dependency-properties system.reflection

所以,我有我的ParseFieldData void类,我在调用我的SetPropertyValue时遇到了一些麻烦(" //这里要返回什么?");我认为它将采用我的FieldName和FieldValue,但我认为这个SetPropertyVaule只接受带双引号的String。

public class CampainProspectsController : ApiController
{
    [HttpPatch]
    public IHttpActionResult Patch([FromBody] JToken Value)
    {
        string toupdate = "";
        Prospects res = new Prospects();
        res.Error = "";
        res.Status = "";
        JArray ar = new JArray();

        var results = JsonConvert.DeserializeObject<UpdateableFieldList>(Value.ToString());



        toupdate = "first_name=" + results.fieldList.FirstOrDefault(e => e.FieldName == "first_name").FieldValue + "&last_name=" + results.fieldList.FirstOrDefault(e => e.FieldName == "last_name").FieldValue
                + "&email=" + results.fieldList.FirstOrDefault(e => e.FieldName == "email").FieldValue + "&phone=" + results.fieldList.FirstOrDefault(e => e.FieldName == "phone").FieldValue + "&id=" + results.fieldList.FirstOrDefault(e => e.FieldName == "id").FieldValue;

        var update = PardotUtilities.Update(toupdate, results.fieldList.FirstOrDefault(e => e.FieldName == "id").FieldValue);

        if (!update.Contains("ok"))
        {
            return BadRequest();
        }

        return Ok();

    }
    [HttpPost]
    public IHttpActionResult Post([FromBody] JToken Value)
    {
        string tocreate = "";

        Prospects res = new Prospects();
        res.Error = "";
        res.Status = "";

        var results = JsonConvert.DeserializeObject<Prospects>(Value.ToString());
        if (results != null)
        {
            // results
            results.id = Guid.NewGuid().ToString();
            tocreate = "first_name=" + results.first_name + "&last_name=" + results.last_name + "&email=" + results.email + "&phone=" + results.phone + "&id=" + results.id;
            var idstr = PardotUtilities.Create(tocreate);
            return Ok(idstr);
        }

        PardotUtilities.Upsert(tocreate, results.id);

        PardotUtilities.Query(tocreate, results.id);
        PardotUtilities.Delete(tocreate, results.id);
        // return Ok(update);

        return Ok();
    }    
}
//public class 

public class Prospects
{

    public String Status { get; set; }
    public String Error { get; set; }

    public string id { get; set; }
    public string email { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string password { get; set; }
    public string company { get; set; }
    public string website { get; set; }

    public string created_at { get; set; }
    public string updated_at { get; set; }
    public List<UpdateFields> fieldList { get; set; }

    public void ParseFieldData(List<UpdateableFieldList> fList)
    {
        Prospects newp = new Prospects();
        //Here is wher setPropertyValue needs to be set  
        newp.SetPropertyValue();   

    }    
}
public static class PropertyExtension
{

    public static void SetPropertyValue(this object obj, string propName, object value)
    {
        obj.GetType().GetProperty(propName).SetValue(obj, value, null);
    }
    public static object GetPropertyValue(this object obj, string propName)
    {
        return obj.GetType().GetProperty(propName).GetValue(obj, null);
    }
}

public class UpdateableFieldList
{
    public List<UpdateFields> fieldList { get; set; }
}
public class UpdateFields
{
    public string FieldName { get; set; }
    public string FieldValue { get; set; }
}

0 个答案:

没有答案