Runtime add DataContract and DataMember to Dynamic Class created using .Net Reflection
I have used .Net Reflection. Emit to create Dynamic Class and its fields. Now when I use Generic List of this dynamic class object in Silverlight enabled WCF service it is giving me an error on the reference side. Service reference does not k开发者_JS百科now about this dynamically created type. This is what I can think of. I am not sure about this? Does anybody have any idea about how to decorate this dynamically created class with DataContract and its' fields with DataMember?
Even if you add DataContract
and DataMember
attributes to a class created using Reflection.Emit at runtime, the contract cannot be included in the service metadata and be discovered by the client, just by adding those attributes, because creating service metadata step is done by inspecting the contract parameters and return values and creating the service description before the service has a chance to run.
One simple option would be to use a weak typed parameter in your service operation, like XElement or the Message class directly
IE:
void SomeOperation(XElement parameter);
void SomeOtherOperation(Message message);
A less simple option, is to use a custom metadata behavior and export metadata for the dynamically generated classes by implementing IWsdlExportExtension to export custom metadata. This works assuming the classes are generated prior to any call to the service and the same class structure is generated at each application run (so might not be an appropriate choice)
You probably want to invoke PropertyBuilder.SetCustomAttribute with a CustomAttributeBuilder based upon the empty DataMemberAttribute constructor. Something similar for the TypeBuilder.
精彩评论