I'm developing an application using C# in .Net Framework 3.5. In the application I create several objects from different dll (also developed in C#) using reflection. All this objects extend an abstract class (AApplication):
private AApplication BuildApplication(string path)
{
Assembly appAssembly = Assembly.LoadFrom(path);
Type[] typeArray = appAssembly.GetTypes();
foreach (Type t in typeArray)
{
if (typeof(AApplication).IsAssignableFrom(t))
{
ConstructorInfo ci = t.GetConstructor(new Type[0]);
AApplication app = ci.Invoke(null) as AApplication;
return app;
}
}
return null;
}
The abstract class AApplication is in another library (named AppsLibrary), which also has images as resources. This class has a simple method to load an image:
protected BitmapImage GetImage(string path)
{
BitmapImage bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.CreateOptions = BitmapCreateOptions.None;
bmp.EndInit();
return bmp;
}
The problem: when I call this method (GetImage) from a class derived from AApplication that is in another dll an exception occurs:
'System.Security.Permissions.MediaPermission, WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) en System.Security.CodeAccessPermission.Demand() en MS.Internal.PresentationCore.SecurityHelper.DemandMediaPermission(MediaPermissionAudio audioPermissionToDemand, MediaPermissionVideo videoPermissionToDemand, MediaPermissionImage imagePermissionToDemand) en System.Windows.Media.Imaging.BitmapDecoder.DemandIfImageBlocked() en System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) en System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() en System.Windows.Media.Imaging.BitmapImage.EndInit() en AppsLibrary.AApplication.GetImage(String path)
The strange thing is that the exception occurs only on some computers. Any idea?