I have a C# class as container for constant values used by the application.
public abstract class MyConstants
{
public abstract class HttpMethods
{
public const string Put = "PUT";
public const string Post = "POST";
public const string Get = "GET";
}
public abstract class ContainerKeys
{
public abstract class GlobalModal
{
public const string DialogId = "6698CB2F-2948-45D9-8902-2C13A7ED6335";
}
}
public const int MaximumImageUploadSize = 3 * 1024 * 1024; // 3MB
public const int MaximumDocumentUploadSize = 5 * 1024 * 1024; // 5MB
}
How can this be converted to a json which can then be copied inside to a javascript-file to use it like MyConstants.HttpMethods.Put inside javascript?
Using
var constants = typeof(MyConstants).GetFields().ToDictionary(x => x.Name, x => x.GetValue(null));
var json = new JavaScriptSerializer().Serialize(constants);
return JavaScript(json + ";");
only returns {"MaximumImageUploadSize":3145728,"MaximumDocumentUploadSize":5242880}; without all the nested classes/constants.
The question seems more related to use reflection on a Type with muliple nested other Types.
Aucun commentaire:
Enregistrer un commentaire