views:

433

answers:

2

I have a Silverlight application which has two different XAPs - an InitialXAP which is loaded statically by the HTML page and a DynamicXAP which is loaded from code within the initial XAP. The DynamicXAP is loaded with code similar to this:

var asm = LoadAssemblyFromXap(stream, "DLLName"); 
// LoadAssemblyFromXAP will load the DynamicXAP as a file stream, 
// unpack it and load DLLName as a dll. 
var controllerType = asm.GetType("ClassNameToInstantiate_InsideAsm");
var constructor = controllerType.GetConstructor(Type.EmptyTypes);
return constructor.Invoke(null);

I have a class which uses reflection (specifically FieldInfo.GetValue) to do data binding. This class is defined in the InitialXAP. If I try to use this class in the DynamicXAP, I get an error:

Message: Unhandled Error in Silverlight Application System.FieldAccessException: Class.In.DynamicXAP.Which.Uses.The.Reflection.Class.In.InitialXAP
   at System.Reflection.RtFieldInfo.PerformVisibilityCheckOnField(IntPtr field, Object target, IntPtr declaringType, FieldAttributes attr, UInt32 invocationFlags)
   at System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck, Boolean doCheckConsistency)
   at System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck)
   at System.Reflection.RtFieldInfo.GetValue(Object obj)

I can get around this error by creating a subclass of the class using reflection and overriding the method using reflection like so:

public class InitialXAP.ClassUsingReflection {

        public virtual object GetValue()
        {
            return fieldInfo.GetValue(parent);
        }
}

public class ClassUsingReflection : InitialXAP.ClassUsingReflection {

        public override object GetValue()
        {
            return fieldInfo.GetValue(parent);
        }
}

But I would prefer to avoid this duplication by allowing reflection from the InitialXAP in the DynamicXAP. Any ideas on what I can do?

+1  A: 

Although there is a learning curve, I would look at Silverlight MEF or Prism (both are together at last in the latest Prism 4 Beta). They both support dynamic loading of modules and enforce good patterns for reuse and separate/team development.

Enough already
-1 for suggesting an alternative? So basically suggesting that this might not be the best way to go about it is not a useful answer. Fair enough. Perhaps critics should actually provide an answer to "that way of doing it" if they feel so strongly about ignoring alternatives :)
Enough already
Plus one, coz it is a viable and better alternative...
Overflow
Explanation for my down vote: I read thru' a fair bit of the Prism doc and I couldn't find anything there which seemed like it would allow me to do cross XAP calls or wire up user controls via convention. (Maybe I missed something and you could point me to the relevant bit?). In the absence of this, it seemed to me that this is not really valid answer for my question. And no, I don't have any better way either - right now, I'm just living with the small amount of duplication, so that I can have auto-binding based on convention - it seems like the lesser evil.
Rohith
A: 

InitialXAP.ClassUsingReflection... Note the duplicate isn't part of the inital xap namespace (ClassUsingReflection),and may be imported. Notice GetVisible-as in not visible to Dynamic xap... Just leave the duplicate(take away base class obviously) and try.

Not applicable
Sorry, I didn't understand your answer. Can you explain a bit more? I don't want to take away the base class - the base class would be used in the InitialXap for the same reason.
Rohith