I have below 2 model classes:
public class InitiateRequest
{
/// <summary>
///
/// </summary>
public string FormCode { get; set; }
/// <summary>
///
/// </summary>
public InitiateFormField Fields { get; set; }
}
public class InitiateFormField
{
[Required]
[JsonPropertyName("STD_INCIDENTTYPEID")]
public string IncidentTypeId { get; set; }
[Required]
public string STD_SUBJECTTYPEID { get; set; }
[Required]
public string STD_SUBJECTID { get; set; }
public string STD_PARENTSUBJECTID { get; set; }
public int PREPARER_ID { get; set; }
public string EXISTING_FORMER_CLIENT { get; set; }
public string MATTER_TITLE { get; set; }
public string MATTER_DESCRIPTION { get; set; }
public int SUBMITTING_ATTORNEY { get; set; }
public int NEW_MATTER_RESPONSIBLE_ATTORNEY { get; set; }
public string MATTER_TYPE { get; set; }
public string MATTER_PRACTICE_CODE { get; set; }
public string NEW_CLIENT_CODE { get; set; }
}
I want to add all the properties from InitiateFormField
class to a Hashtable.
var properties = new Hashtable();
Hashtable Key is the name of the model property and value is the actual value of the property.
E.g.
properties.Add("STD_INCIDENTTYPEID", "myString");
Is there a way to do this?
CodePudding user response:
I do not understand why one should use HashTable
instead of a Dictionary, but I'm digressing.
Given your code, I assume you have access to Json.NET.
You can convert any Json object to a Dictionary<string, object>
, so you can:
var serializedFields = JsonConvert.SerializeObject(request.Fields);
var fieldsDictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>();
// if you _really_ need a hashTable:
var hashTable = new HashTable(fieldsDictionary);
There are possibly more efficient ways, but given the optimization of Json.NET and the fact that you are guaranteed to use only serializable types, I think this is the simplest solution.