Serializing / Deserializing System.Uri - serialization

I've been using RavenDB for all of two hours, so apologies if I've missed something obvious.
I'm storing a denormalized view model with a property of type System.Uri. The Uri is serialized as a string, which is OK I guess, but throws this exception when I load the document:
Message=Could not cast or convert from System.String to System.Uri.
Source=Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Utilities\ConvertUtils.cs:line 267
at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Utilities\ConvertUtils.cs:line 244
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:line 544

Got it! There are two secrets. The first is to create a JsonConverter for the Uri type.
public class UriJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return object.Equals(objectType, typeof (Uri));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.String:
return CreateUri((string) reader.Value);
case JsonToken.Null:
return null;
default:
var msg = string.Format("Unable to deserialize Uri from token type {0}", reader.TokenType);
throw new InvalidOperationException(msg);
}
}
private static Uri CreateUri(string uriString)
{
Uri uri;
if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri))
if (!Uri.TryCreate(uriString, UriKind.Absolute, out uri))
if (!Uri.TryCreate(uriString, UriKind.RelativeOrAbsolute, out uri))
{
var msg = string.Format("Unable to determine proper UriKind for Uri {0}", uriString);
throw new InvalidOperationException(msg);
}
return uri;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (null == value)
{
writer.WriteNull();
return;
}
var uri = value as Uri;
if (uri != null)
{
writer.WriteValue(uri.OriginalString);
return;
}
var msg = string.Format("Unable to serialize {0} with {1}", value.GetType(), typeof (UriJsonConverter));
throw new InvalidOperationException(msg);
}
}
The second is to register the converter with the RavenDB serializer.
private static DocumentStore OpenStore()
{
var store = new DocumentStore()
{
ConnectionStringName = "RavenDB"
};
store.Conventions.CustomizeJsonSerializer = CustomJsonSerializer;
store.Initialize();
return store;
}
private static void CustomJsonSerializer(JsonSerializer serializer)
{
serializer.Converters.Add(new UriJsonConverter());
}

I'm not positive what Newtonsoft is doing under the covers, but if it's calling new System.Uri("/about-us") without specifying UriKind.Relative in the constructor a UriFormatException will be thrown.
So, depending on how your Uri is being created in your model, it might be an option for you to ensure it's absolute before storing it.
I'm not positive, but I would think a Uri like new System.Uri("http://foo.com/about-us") would be stored as "http://foo.com/about-us" and would convert OK when it comes back out.

Related

JsonConverterAttribute is not working for Deserialization in ASP.NET Core 3.1 / 5.0

I want set property names at runtime. I already achieve this for serialization.
For example. I have a simple model like as below:
[JsonConverter(typeof(DataModelConverter))]
public class DataModel
{
public string Name { get; set; }
public int Age { get; set; }
}
And I have a simple DataModelConverter, that inherited from JsonConverter:
public class DataModelConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
Type type = value.GetType();
JObject jo = new JObject();
foreach (PropertyInfo prop in type.GetProperties())
{
jo.Add(prop.Name == "Name" ? "FullName" : prop.Name, new JValue(prop.GetValue(value)));
}
jo.WriteTo(writer);
}
public override bool CanRead
{
get { return false; }
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DataModel);
}
}
And I have a simple controller like as below:
[Route("api/[controller]")]
[ApiController]
public class NewtonController : ControllerBase
{
public IEnumerable<DataModel> GetNewtonDatas([FromBody] DataModel input)
{
return new List<DataModel>()
{
new DataModel
{
Name="Ramil",
Age=25
},
new DataModel
{
Name="Yusif",
Age=26
}
};
}
}
If I call this API, result will like as below (Showing FullName Instead of Name):
[
{
"FullName": "Ramil",
"Age": 25
},
{
"FullName": "Yusif",
"Age": 26
}
]
But I have a problem. This is not working for deserialization.
For example: If I call this API with this body, then Name will null.
{
"FullName":"Ramil"
}
My attribute is not working for deserialization. I want set property name via attribute for deserialization at runtime .
I don't want use some middleware, I want to achieve this only by using the any attribute at runtime. I must read JSON property names from my appsettings.json file.
Thanks for help!
You have overridden CanRead to return false:
public override bool CanRead
{
get { return false; }
}
This causes Json.NET not to call your your converter's DataModelConverter.ReadJson() method during deserialization, and instead use default deserialization. Since "FullName" does not have the same (case-invariant) name as the Name property, it never gets set, and remains null.
To fix this, remove the override for CanRead (the default implementation returns true) and implement ReadJson(), e.g. as follows:
public class DataModelConverter : NameRemappingConverterBase
{
static string AlternateName => "FullName";
static string OriginalName => "Name";
public override bool CanConvert(Type objectType) => objectType == typeof(DataModel);
// Replace the below logic with name mappings from appsettings.json
protected override string ToJsonPropertyName(JsonProperty property) =>
string.Equals(property.UnderlyingName, OriginalName, StringComparison.OrdinalIgnoreCase) ? AlternateName : base.ToJsonPropertyName(property);
protected override string FromJsonPropertyName(string name) =>
string.Equals(name, AlternateName, StringComparison.OrdinalIgnoreCase) ? OriginalName : base.FromJsonPropertyName(name);
}
public abstract class NameRemappingConverterBase : JsonConverter
{
protected virtual string ToJsonPropertyName(JsonProperty property) => property.PropertyName;
protected virtual string FromJsonPropertyName(string name) => name;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.MoveToContentAndAssert().TokenType == JsonToken.Null)
return null;
if (reader.TokenType != JsonToken.StartObject)
throw new JsonSerializationException(string.Format("Unexpected token {0}", reader.TokenType));
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(objectType);
var value = existingValue ?? contract.DefaultCreator();
while (reader.ReadToContentAndAssert().TokenType != JsonToken.EndObject)
{
if (reader.TokenType != JsonToken.PropertyName)
throw new JsonSerializationException(string.Format("Unexpected token {0}", reader.TokenType));
var name = FromJsonPropertyName((string)reader.Value);
reader.ReadToContentAndAssert();
var property = contract.Properties.GetProperty(name, StringComparison.OrdinalIgnoreCase);
if (!ShouldDeserialize(property))
{
reader.Skip();
}
else
{
var propertyValue = serializer.Deserialize(reader, property.PropertyType);
property.ValueProvider.SetValue(value, propertyValue);
}
}
return value;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var contract = (JsonObjectContract)serializer.ContractResolver.ResolveContract(value.GetType());
writer.WriteStartObject();
foreach (var property in contract.Properties.Where(p => ShouldSerialize(p, value)))
{
var propertyValue = property.ValueProvider.GetValue(value);
if (propertyValue == null && serializer.NullValueHandling == NullValueHandling.Ignore)
continue;
var name = ToJsonPropertyName(property);
writer.WritePropertyName(name);
serializer.Serialize(writer, propertyValue);
}
writer.WriteEndObject();
}
protected virtual bool ShouldDeserialize(JsonProperty property) =>
property != null && property.Writable;
protected virtual bool ShouldSerialize(JsonProperty property, object value) =>
property.Readable && !property.Ignored && (property.ShouldSerialize == null || property.ShouldSerialize(value));
}
public static partial class JsonExtensions
{
public static JsonReader ReadToContentAndAssert(this JsonReader reader) =>
reader.ReadAndAssert().MoveToContentAndAssert();
public static JsonReader MoveToContentAndAssert(this JsonReader reader)
{
if (reader == null)
throw new ArgumentNullException();
if (reader.TokenType == JsonToken.None) // Skip past beginning of stream.
reader.ReadAndAssert();
while (reader.TokenType == JsonToken.Comment) // Skip past comments.
reader.ReadAndAssert();
return reader;
}
public static JsonReader ReadAndAssert(this JsonReader reader)
{
if (reader == null)
throw new ArgumentNullException();
if (!reader.Read())
throw new JsonReaderException("Unexpected end of JSON stream.");
return reader;
}
}
Demo fiddle here.

How to setup Json.NET's Converters depending on custom request's header

I need to use different Json.NET's JSON Converters depends on header.
Some think like this:
services
.AddMvcCore()
.AddJsonOptions(options =>
{
// If(my_custom_header_value == "use_first_converter")
options.SerializerSettings.Converters.Add(new FirstConverter());
// Else
//options.SerializerSettings.Converters.Add(new FirstConverter());
})
For converting depending on custom requests' header, it is impossible to setup by AddJsonOptions. You could not access the HttpContext during ConfigureServices since there is no request during this process.
For a workaround, try register IHttpContextAccessor like
public class FirstConverter : JsonConverter
{
private readonly IHttpContextAccessor _httpContextAccessor;
public FirstConverter(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public override bool CanConvert(Type objectType)
{
var header = _httpContextAccessor.HttpContext.Request.Headers;
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And then in ConfigureServices
services.AddMvc().AddJsonOptions(options =>
{
var httpContextAccessor = services.BuildServiceProvider().GetRequiredService<IHttpContextAccessor>();
// If(my_custom_header_value == "use_first_converter")
options.SerializerSettings.Converters.Add(new FirstConverter(httpContextAccessor));
// Else
//options.SerializerSettings.Converters.Add(new FirstConverter());
});
Check wether to convert by var header = _httpContextAccessor.HttpContext.Request.Headers;

Newtonsoft Json.NET Ignore a member at runtime in Json converter or invoking JsonConverter SerializeObject

Let's say I have the following custom JsonConverter for serialization and/or deserialization:
public class VersionConverter : JsonConverter<Version>
{
public override void WriteJson(JsonWriter writer, Version value, JsonSerializer serializer)
{
writer.WriteValue(value.ToString());
}
public override Version ReadJson(JsonReader reader, Type objectType, Version existingValue, bool hasExistingValue, JsonSerializer serializer)
{
string s = (string)reader.Value;
return new Version(s);
}
}
public class NuGetPackage
{
public string PackageId { get; set; }
public Version Version { get; set; }
public string Description { get; set; }
}
Let's say I have the following code snippet in my application:
NuGetPackage p1 = new NuGetPackage
{
PackageId = "Newtonsoft.Json",
Version = new Version(10, 0, 4),
Description = null
};
string json = JsonConvert.SerializeObject(p1, Formatting.Indented, new VersionConverter());
I want the Json.NET converter to Ignore the Description member variable of the NuGetPackage class.
Note: I do Not Want to use the following "marker boolean" member variable:
public bool ShouldSerializeINSERT_YOUR_PROPERTY_NAME_HERE()
{
if(someCondition){
return true;
}else{
return false;
}
}
I would rather specify the ignoring of a specific member variable somewhere
a) when my code invokes the JsonConvert.SerializeObject?
b) or within the VersionConverter code class itself?
Could someone please show me how to ignore the specific member variable in such a way?
Since NuGetPackage is fairly simple, you could just write an additional JsonConverter for NuGetPackage that serializes only the members you need, e.g.:
public class SimplifiedNuGetPackageConverter : JsonConverter
{
public override bool CanConvert(Type objectType) { return objectType == typeof(NuGetPackage); }
public override bool CanRead { get { return false; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var package = (NuGetPackage)value;
serializer.Serialize(writer, new { package.PackageId, package.Version });
}
}
Then serialize as follows:
var settings = new JsonSerializerSettings
{
Converters = { new VersionConverter() },
};
if (!someCondition)
settings.Converters.Add(new SimplifiedNuGetPackageConverter());
var json = JsonConvert.SerializeObject(p1, Formatting.Indented, settings);
If you are serializing multiple instances of NuGetPackage at once and need to write Description for some but not all, you could add the logic for someCondition inside WriteJson() itself:
public class ConditionalNuGetPackageConverter : JsonConverter
{
public override bool CanConvert(Type objectType) { return objectType == typeof(NuGetPackage); }
public override bool CanRead { get { return false; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var package = (NuGetPackage)value;
// Replace with your logic:
var someCondition = !string.IsNullOrWhiteSpace(package.Description);
if (someCondition)
serializer.Serialize(writer, new { package.PackageId, package.Version, package.Description });
else
serializer.Serialize(writer, new { package.PackageId, package.Version });
}
}
And then serialize as follows:
var settings = new JsonSerializerSettings
{
Converters = { new ConditionalNuGetPackageConverter(), new VersionConverter() },
};
var json = JsonConvert.SerializeObject(p1, Formatting.Indented, settings);
Working .Net fiddle here.

WCF Cannot deserialize when using IDataContractSurrogate

I'm using WCF service with WebHttpBinding. I have written custom IDataContractSurrogate implementation to serialize enum as strings.
Enums are serialization WORKS, but deserialization fails.
when request contains enum, then I get:
The remote server returned an unexpected response: (400) Bad Request.
or when response contains an enum, then I get:
InvalidCastException: Specified cast is not valid
Server stack trace:
at ReadMyResponseFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] )
at System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserializeWithSurrogate(XmlReaderDelegator xmlReader, Type declaredType, DataContract surrogateDataContract, String name, String ns)
at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
at ...
the server code:
string baseAddress = "http://localhost:8733/Design_Time_Addresses/FingerprintService/";
_serviceHost = new WebServiceHost(myServiceInstance, new Uri(baseAddress));
_serviceHost.AddServiceEndpoint(typeof (IMyService), new WebHttpBinding(WebHttpSecurityMode.None), baseAddress);
EndpointExtension.Setup(_serviceHost.Description.Endpoints[0]);
_serviceHost.Open();
client code:
IMyService FingerprintService()
{
var channelFaftory = new WebChannelFactory<IMyService>(new Uri(TbxUri.Text));
EndpointExtension.Setup(channelFaftory.Endpoint);
return channelFaftory.CreateChannel();
}
the endpoint setup (common for both host and client):
public static void Setup(ServiceEndpoint endpoint)
{
var webHttpBehavior = endpoint.Behaviors.Find<WebHttpBehavior>();
if (webHttpBehavior == null)
{
webHttpBehavior = new WebHttpBehavior();
endpoint.Behaviors.Add(webHttpBehavior);
}
foreach (OperationDescription opertion in endpoint.Contract.Operations)
{
var dataContractBehavior = opertion.Behaviors.Find<DataContractSerializerOperationBehavior>();
dataContractBehavior.DataContractSurrogate = new EnumSurrogate();
}
}
}
and finaly, the surrogate:
public class EnumSurrogate : IDataContractSurrogate
{
public Type GetDataContractType(Type type)
{
if (type.IsEnum)
{
return typeof(string);
}
return type;
}
public object GetObjectToSerialize(object obj, Type targetType)
{
if (obj is Enum)
{
return obj.ToString();
}
return obj;
}
public object GetDeserializedObject(object obj, Type targetType)
{
if (obj is string && targetType.IsEnum)
{
return Enum.Parse(targetType, (string)obj);
}
return obj;
}
//other methods throws NotImplementedException
}
your approach works nicely when serializing, but will not work when deserializing. This will fail because primitive values cannot be surrogated while deserializing.
https://canbilgin.wordpress.com/2012/06/07/how-to-serialize-an-enum-as-string-with-idatacontractsurrogate/

Creating WCF Service at runtime

We are going to build a web service from metadata read at runtime. I mean the entire web service: the signatures, contracts and implementation.
There are two main paths I see from here.
The first path is that you generate code. Either you generate C# code in strings and compile it on the fly or more elegantly (and complicatedly), you emit MSIL code. This way you have WCF code and WCF will take care of generating the WSDL from it.
The second path is to use a generic service. A service with an operation Message Process(Message) accepting everything. We still want to expose the service as a 'normal' service, so I would need a WSDL somewhere. How can I create a WSDL? I thought about using System.ServiceModel.Description until I realised that deep inside, this API depends on concrete types. With this approach we wouldn't have any data contract type and would process XML on the fly, using metadata to interpret it. So we need somehow to generate the WSDL. Is that a crazy idea? WSDL has quite a complicated spec...
A third option would be to use an hybrid approach, emitting types just to create signatures but implementing the service using non-emitted code (reflecting on emitted types). Weird, but might be simpler than hand crafting WSDL by hand...
Suggestions?
It's a pain to do, but possible to emitting types. Create your .svc and point it at your custom ServiceHostFactory:
<%# ServiceHost Language="C#" Debug="true" Factory="Foo.FooServiceHostFactory" %>
[ServiceContract]
public class FooService { }
Your ServiceHostFactory is where you generate your operation contracts, types that go with it, etc:
public class FooServiceHostFactory : ServiceHostFactory {
public override System.ServiceModel.ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) {
ServiceHost serviceHost = new FooServiceHost(baseAddresses);
serviceHost.AddDefaultEndpoints();
GenerateServiceOperations(serviceHost);
return serviceHost;
}
private void GenerateServiceOperations(ServiceHost serviceHost) {
var methodNames = new[] {
new { Name = "Add" },
new { Name = "Subtract" },
new { Name = "Multiply" }
};
foreach (var method in methodNames) {
foreach (var endpoint in serviceHost.Description.Endpoints) {
var contract = endpoint.Contract;
var operationDescription = new OperationDescription("Operation" + method.Name, contract);
var requestMessageDescription = new MessageDescription(string.Format("{0}{1}/Operation{2}", contract.Namespace, contract.Name, method.Name), MessageDirection.Input);
var responseMessageDescription = new MessageDescription(string.Format("{0}{1}/Operation{2}Response", contract.Namespace, contract.Name, method.Name), MessageDirection.Output);
var elements = new List<FooDataItem>();
elements.Add(new FooDataItem { Name = "X", DataType = typeof(int) });
elements.Add(new FooDataItem { Name = "Y", DataType = typeof(int) });
//note: for a complex type it gets more complicated, but the same idea using reflection during invoke()
//object type = TypeFactory.CreateType(method.Name, elements);
//var arrayOfType = Array.CreateInstance(type.GetType(), 0);
//var parameter = new MessagePartDescription(method.Name + "Operation", contract.Namespace);
//parameter.Type = arrayOfType.GetType();
//parameter.Index = 0;
//requestMessageDescription.Body.Parts.Add(parameter);
var retVal = new MessagePartDescription("Result", contract.Namespace);
retVal.Type = typeof(int);
responseMessageDescription.Body.ReturnValue = retVal;
int indexer = 0;
foreach (var element in elements) {
var parameter = new MessagePartDescription(element.Name, contract.Namespace);
parameter.Type = element.DataType;
parameter.Index = indexer++;
requestMessageDescription.Body.Parts.Add(parameter);
}
operationDescription.Messages.Add(requestMessageDescription);
operationDescription.Messages.Add(responseMessageDescription);
operationDescription.Behaviors.Add(new DataContractSerializerOperationBehavior(operationDescription));
operationDescription.Behaviors.Add(new FooOperationImplementation());
contract.Operations.Add(operationDescription);
}
}
}
protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) {
return base.CreateServiceHost(serviceType, baseAddresses);
}
}
In the ServiceHostFactory you define the behaviors along with the metadata, so your behavior would need to implement IOperationBehavior and IOperationInvoker (or you could implement them separately) and look something like this:
public class FooOperationImplementation : IOperationBehavior, IOperationInvoker {
OperationDescription operationDescription;
DispatchOperation dispatchOperation;
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters) {
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) {
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation) {
this.operationDescription = operationDescription;
this.dispatchOperation = dispatchOperation;
dispatchOperation.Invoker = this;
}
public void Validate(OperationDescription operationDescription) {
}
public object[] AllocateInputs() {
return new object[2];
}
public object Invoke(object instance, object[] inputs, out object[] outputs) {
//this would ALL be dynamic as well depending on how you are creating your service
//for example, you could keep metadata in the database and then look it up, etc
outputs = new object[0];
switch (operationDescription.Name) {
case "OperationAdd":
return (int)inputs[0] + (int)inputs[1];
case "OperationSubtract":
return (int)inputs[0] - (int)inputs[1];
case "OperationMultiply":
return (int)inputs[0] * (int)inputs[1];
default:
throw new NotSupportedException("wtf");
}
}
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state) {
throw new NotImplementedException("Method is not asynchronous.");
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result) {
throw new NotImplementedException("Method is not asynchronous.");
}
public bool IsSynchronous {
get { return true; }
}
}
For complex types, this is where you need to make a judgement call, which is the root of your question, is how to emit the types. Here's an example, but you can do it any way you can see fit. I'm calling this in my ServiceHostFactory (warning: it's demo code)
static public class TypeFactory {
static object _lock = new object();
static AssemblyName assemblyName;
static AssemblyBuilder assemblyBuilder;
static ModuleBuilder module;
static TypeFactory() {
lock (_lock) {
assemblyName = new AssemblyName();
assemblyName.Name = "FooBarAssembly";
assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
module = assemblyBuilder.DefineDynamicModule("FooBarModule");
}
}
static public object CreateType(string typeName, List<FooDataItem> elements) {
TypeBuilder typeBuilder = module.DefineType(typeName, TypeAttributes.Public | TypeAttributes.Class);
foreach(var element in elements) {
string propertyName = element.Name;
Type dataType = element.DataType;
FieldBuilder field = typeBuilder.DefineField("_" + propertyName, dataType, FieldAttributes.Private);
PropertyBuilder property =
typeBuilder.DefineProperty(propertyName,
PropertyAttributes.None,
dataType,
new Type[] { dataType });
MethodAttributes GetSetAttr =
MethodAttributes.Public |
MethodAttributes.HideBySig;
MethodBuilder currGetPropMthdBldr =
typeBuilder.DefineMethod("get_value",
GetSetAttr,
dataType,
Type.EmptyTypes);
ILGenerator currGetIL = currGetPropMthdBldr.GetILGenerator();
currGetIL.Emit(OpCodes.Ldarg_0);
currGetIL.Emit(OpCodes.Ldfld, field);
currGetIL.Emit(OpCodes.Ret);
MethodBuilder currSetPropMthdBldr =
typeBuilder.DefineMethod("set_value",
GetSetAttr,
null,
new Type[] { dataType });
ILGenerator currSetIL = currSetPropMthdBldr.GetILGenerator();
currSetIL.Emit(OpCodes.Ldarg_0);
currSetIL.Emit(OpCodes.Ldarg_1);
currSetIL.Emit(OpCodes.Stfld, field);
currSetIL.Emit(OpCodes.Ret);
property.SetGetMethod(currGetPropMthdBldr);
property.SetSetMethod(currSetPropMthdBldr);
}
Type generetedType = typeBuilder.CreateType();
return Activator.CreateInstance(generetedType);
}
}
update: I wrote a quick example and it is available here.