mardi 18 juillet 2017

trying to use reflection in C# with static class Property Extension with Set & Get property vale

So, I have my ParseFieldData void class and I am having some trouble calling my SetPropertyValue(" //What to return in here? "); I'm thinking it will take my FieldName and FieldValue but I am thinking this SetPropertyVaule only takes String with double quotes.

  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; }
        }

    }





Aucun commentaire:

Enregistrer un commentaire