Ok Assuming you know about reflection and create a new AppDomain . What I know, you know how to do it ... :)
I created two helper classes that allow you to pass anonymous objects. ProxyAnonymousObject and ProxyDynamicObject . You create a ProxyAnonymousObject in the first AppDomain and use ProxyDynamicObject in another AppDomain . (both of these objects exist in the primary AppDomain library)
[Serializable] public class ProxyAnonymousObject : ISerializable { static Dictionary<string, Type> cached = new Dictionary<string, Type>(); object model; public Dictionary<string, object> ModelProperties = new Dictionary<string, object>(); public ProxyAnonymousObject(object model) { this.model = model; } public ProxyAnonymousObject(SerializationInfo info, StreamingContext ctx) { try { string fieldName = string.Empty; object fieldValue = null; foreach (var field in info) { fieldName = field.Name; fieldValue = field.Value; if (string.IsNullOrWhiteSpace(fieldName)) continue; if (fieldValue == null) continue; ModelProperties.Add(fieldName, fieldValue); } } catch (Exception e) { var x = e; } } public void GetObjectData(SerializationInfo info, StreamingContext context) { foreach (var pi in model.GetType().GetProperties()) { info.AddValue(pi.Name, pi.GetValue(model, null), pi.PropertyType); } } } public class ProxyDynamicObject : DynamicObject{ internal ProxyAnonymousObject Proxy { get; set; } public ProxyDynamicObject(ProxyAnonymousObject model) { this.Proxy = model; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = Proxy.ModelProperties[binder.Name]; return true; } }
To make this work in your inherited MarshalByRefObject class, you simply set the dynamic object target to new ProxyDynamicObject(model) . In the example I wrote, I make such a call.
instance = Activator.CreateInstance(type); var setModel = type.GetMethod("SetModel", BindingFlags.Public | BindingFlags.Instance); var render = type.GetMethod("Render", BindingFlags.Public | BindingFlags.Instance); setModel.Invoke(instance, new object[] { new ProxyDynamicObject(model) }); render.Invoke(instance, null);
I wrote a blog post about this http://buildstarted.com/2011/06/28/getting-anonymous-types-to-cross-the-appdomain-boundary/ to explain this in a bit more detail. (although I am not very good)
There are certain problems with this implementation. It does not support nested anonymous types, and I'm sure it will break at all. But it will definitely help you on the right track.
source share