I have created a program to load dynamic assemblies using the following code:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
namespace BarcodeReader
{
public class Parsing
{
private static string _FolderName = "BarcodeReaders";
private static bool _Initialized = false;
private static IEnumerable<IBarcodeReader> _Objs;
/// Parse the picture
/// <returns>The value from the picture</returns>
public static async Task<string> ParsePicture()
{
// Check if this class has not been initialized, and if it hasn't initialize it
if (!_Initialized)
{
await InitializeAsync();
}
foreach (var Obj in _Objs)
{
if (Obj.IsType())
{
return Obj.GetValue();
}
}
return null;
}
private static async Task InitializeAsync()
{
// Get the folder
var Folder = await GetFolder();
// Get the Files in the Folder
var Files = await Folder.GetFilesAsync();
// Initialize the objects and set them
_Objs = InitializeObjects(Files);
// Set it as initialized
_Initialized = true;
}
private static IEnumerable<IBarcodeReader> InitializeObjects(IEnumerable<Windows.Storage.StorageFile> Files)
{
foreach (var File in Files)
{
string Name = File.Path;
var Assembly = System.Reflection.Assembly.Load(new AssemblyName(Name));
foreach (var Typ in Assembly.ExportedTypes)
{
var TypInfo = Typ.GetTypeInfo();
foreach (var Interf in TypInfo.ImplementedInterfaces)
{
if (Interf.Name.Equals("IBarcodeReader"))
{
yield return (IBarcodeReader)Activator.CreateInstance(Typ);
}
}
}
}
}
private static async Task<bool> BarcodeFolderExist(Windows.Storage.StorageFolder Folder)
{
// Get all folders
var Folders = await Folder.GetFoldersAsync();
// For each folder, check if it is the Folder we are searching and if it is return true
foreach (var Foldr in Folders)
{
if (Foldr.Name.Equals(_FolderName))
{
return true;
}
}
// Return false as the folder was not found
return false;
}
private static async Task<Windows.Storage.StorageFolder> GetFolder()
{
// Get the local-folder
var Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
// Check if the folder does not exist, and if it does not create it
if (!await BarcodeFolderExist(Folder))
{
await Folder.CreateFolderAsync(_FolderName);
}
return await Folder.GetFolderAsync(_FolderName);
}
}
}
And the project I am trying to load is these files
namespace QRReader
{
public sealed class QRReader : IBarcodeReader
{
public bool IsType()
{
return true;
}
public string GetValue()
{
return "HEJ";
}
}
public interface IBarcodeReader
{
bool IsType();
string GetValue();
}
}
But I get this error
FileLoadException was unhandled by user code
The assembly name or code base was illegal. (Exception HRESULT: 0x80131047)
The name-variable is set to
C:\Users\Lasse\AppData\Local\Packages\93e3b2c9-7ef8-4537-be39-d0f3e93ca100_e85ydygyad1dy\LocalState\BarcodeReaders\QRReader.winmd
Everything I've read on the internet says that Microsoft have made it a deliberate security feature of the runtime environment (WinRT, and UWP) that it be impossible to load assemblies at runtime. This is a show stopping limiting feature in UWP. It more or less renders the platforms useless because if there are customisations for a given customer, the application vendor would have to split the app and deploy it a version to the store for each customer.
Please take the time to up vote this feature request on allowing assemblies to be loaded dynamically at runtime:
https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/18145291-dynamically-load-assembly
Related
I have a property Files of type ObservableCollection<Element> in my Application class.
public sealed partial class App : Application
{
public string CurrentPath;
public ObservableCollection<Element> Files;
public async void RefreshFilesAsync()
{
Files.Clear();
var folder = await StorageFolder.GetFolderFromPathAsync(CurrentPath);
var foldersRequest = folder.GetFoldersAsync();
var filesRequest = folder.GetFilesAsync();
var folders = await foldersRequest;
var files = await filesRequest;
foreach (StorageFolder directory in folders)
{
Files.Add(new FolderElement(directory));
}
foreach (StorageFile file in files)
{
Files.Add(new FileElement(file));
}
}
// ...
}
Element is a wrapper class of mine for StorageFile objects. This class is made so that setting a new value to the Name property calls the API to rename the actual file.
public class Element
{
public string Name
{
get => StorageFile.Name;
set =>
{
StorageFolder.RenameAsync(value, NameCollisionOption.GenerateUniqueName).Completed = (IAsyncAction act, AsyncStatus status) =>
{
(App.Current as App).RefreshFilesAsync();
};
}
}
public StorageFile StorageFile { get; }
//...
}
I have a DataGrid linked to the Application property Files, so you can deduce that whenever I rename the column for the file name, the API is called to perform the real renaming.
Here's when my problem occur. The API call succeeds and the file is renamed, but inside RefreshFilesAsync(), when Files.Clear(); is called, I get an exception The application called an interface that was marshalled for a different thread.
What am I getting wrong?
To run code with the UI thread, I used Window.Current.Dispatcher.RunAsync(), and put it in a separate async method. The exception does not appear anymore.
public string Name
{
get => StorageFolder.Name;
set => RenameAsync(value);
}
private async void RenameAsync(string newName)
{
await StorageFolder.RenameAsync(newName, NameCollisionOption.GenerateUniqueName);
await Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => (App.Current as App).RefreshFilesAsync());
}
Currently i've got this code:
private async Task<bool> IsMentionedInDisposeCallAsync(SyntaxNodeAnalysisContext context, FieldDeclarationSyntax fieldDeclarationSyntax)
{
foreach (var variableDeclaratorSyntax in fieldDeclarationSyntax.Declaration.Variables)
{
var declaredSymbol = context.SemanticModel.GetDeclaredSymbol(variableDeclaratorSyntax);
if (declaredSymbol is IFieldSymbol fieldSymbol)
{
// SymbolFinder.FindReferencesAsync()
var b = fieldSymbol.Locations;
// context.SemanticModel.Compilation.
}
}
return false;
}
And this scenario:
private static readonly string TestSourceImplementsDisposableAndDoesMentionDisposableField = #"
using System;
using System.IO;
namespace ConsoleApplication1
{
public class SampleDisposable : IDisposable
{
public void Dispose()
{
}
}
public class SampleConsumer : IDisposable
{
private SampleDisposable _disposable = new SampleDisposable();
private IDisposable _ms = new MemoryStream();
public void Dispose()
{
_disposable?.Dispose();
_ms?.Dispose();
}
}
}";
Ultimately my desire is to figure out whether a dispose method is accessing a disposable field. Unfortunately i can't seem to find a way to get this working without using SymbolFinder, which requires a solution.
I did something similar with SymbolFinder and it was an easy thing to do - but how do i do it from the functionality available within a diagnostic?
Am i missing something obvious here?
You could simply use the SemanticModel to analyse the type used for the field like this:
private async Task<bool> IsMentionedInDisposeCallAsync(SyntaxNodeAnalysisContext context, FieldDeclarationSyntax fieldDeclarationSyntax)
{
foreach (var variableDeclaratorSyntax in fieldDeclarationSyntax.Declaration.Variables)
{
var declaredSymbol = context.SemanticModel.GetDeclaredSymbol(variableDeclaratorSyntax);
if (declaredSymbol is IFieldSymbol fieldSymbol)
{
var isDisposeable = CheckIsTypeIDisposeable(fieldSymbol.Type as INamedTypeSymbol);
// SymbolFinder.FindReferencesAsync()
var b = fieldSymbol.Locations;
// context.SemanticModel.Compilation.
}
}
return false;
}
private string fullQualifiedAssemblyNameOfIDisposeable = typeof(IDisposable).AssemblyQualifiedName;
private bool CheckIsTypeIDisposeable(INamedTypeSymbol type)
{
// Identify the IDisposable class. You can use any method to do this here
// A type.ToDisplayString() == "System.IDisposable" might do it for you
if(fullQualifiedAssemblyNameOfIDisposeable ==
type.ToDisplayString() + ", " + type.ContainingAssembly.ToDisplayString())
{
return true;
}
if(type.BaseType != null)
{
if (CheckIsTypeIDisposeable(type.BaseType))
{
return true;
}
}
foreach(var #interface in type.AllInterfaces)
{
if (CheckIsTypeIDisposeable(#interface))
{
return true;
}
}
return false;
}
Basically you would search through all interfaces of the class and the base class recursively to find the type corresponding to IDisposeable - which should be somewhere in the hierarchy.
I am trying to add support for System.Web.Mvc.HtmlHelper to a CLI app for compiling Razor templates, but although it compiles it fails at runtime with:
System.TypeLoadException: Could not load type 'HtmlHelper`1' from assembly '/Users/oligofren/src/razor-cli/build/System.Web.Mvc.dll'.
How should I proceed in fixing this?
I am not well versed in the core of .NET (here in Mono version), so I can't say if I have done anything wrong here. I have added all the assemblies to the build folder (where the exe ends up) and I also try to manually load the required assemblies before RazorEngine tries to compile the assemblies.
How can I resolve this?
Full source code
// See also tips on building cli apps with razorengine: https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Hosts.Console/RazorEngine.Hosts.Console.csproj
using System;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using Moq;
using System.IO;
using Newtonsoft.Json.Linq;
using RazorEngine;
using RazorEngine.Templating; // For extension methods.
using RazorEngine.Configuration;
using RazorEngine.Text;
public class RazorCli
{
static public void Main (string[] args)
{
CheckCommandLine(args);
string template = ReadFile(args[0]);
JObject model = ParseModel(args[1]);
// try to load the required assemblies
//http://stackoverflow.com/a/23496144/200987
System.Reflection.Assembly.Load("System.Web");
System.Reflection.Assembly.Load("System.Web.Mvc");
var result = CompileTemplate(template, model);
Console.WriteLine (result);
}
private static string CompileTemplate (string template, JObject model)
{
string res = "";
var config = new TemplateServiceConfiguration();
// You can use the #inherits directive instead (this is the fallback if no #inherits is found).
config.BaseTemplateType = typeof(MyClassImplementingTemplateBase<>);
try
{
using (var service = RazorEngineService.Create(config))
{
res = service.RunCompile(template, "templateKey", null, model);
}
}
catch( RazorEngine.Templating.TemplateCompilationException ex )
{
Console.WriteLine (ex);
System.Environment.Exit(1);
}
return res;
}
/* Cannot dispatch a dynamic object to extension methods */
private static JObject ParseModel(string fileName){
string json = ReadFile(fileName);
return JObject.Parse(json);
}
private static void CheckCommandLine(string[] args){
if(args.Length != 2){
Usage();
System.Environment.Exit(1);
}
}
private static void Usage(){
string usage = "Usage: razor-cli <partial.cshtml> <model.json>\n";
Console.WriteLine(usage);
}
private static String ReadFile(string filename)
{
string result;
using (StreamReader sr = new StreamReader(filename))
{
result = sr.ReadToEnd();
}
return result;
}
}
public class MyHtmlHelper
{
public IEncodedString Raw(string rawString)
{
return new RawString(rawString);
}
}
// https://antaris.github.io/RazorEngine/TemplateBasics.html
public abstract class MyClassImplementingTemplateBase<T> : TemplateBase<T>
{
public MyClassImplementingTemplateBase()
{
Html = MvcHelpers.CreateHtmlHelper<Object>();
}
public HtmlHelper Html { get; set; }
}
// Ripped straight from a SO Q/A
// http://stackoverflow.com/questions/17271688/mocking-viewcontext-to-test-validation-error-messages
public class MvcHelpers {
public static HtmlHelper<TModel> CreateHtmlHelper<TModel>(ViewDataDictionary dictionary = null)
{
if (dictionary == null)
dictionary = new ViewDataDictionary { TemplateInfo = new TemplateInfo() };
var mockViewContext = new Mock<ViewContext>(
new ControllerContext(
new Mock<HttpContextBase>().Object,
new RouteData(),
new Mock<ControllerBase>().Object),
new Mock<IView>().Object,
dictionary,
new TempDataDictionary(),
new Mock<TextWriter>().Object);
var mockViewDataContainer = new Mock<IViewDataContainer>();
mockViewDataContainer.Setup(v => v.ViewData).Returns(dictionary);
return new HtmlHelper<TModel>(mockViewContext.Object, mockViewDataContainer.Object);
}
}
Details on how I run this can be seen in the Makefile, if that helps.
Further details
Installed Mono 4.2.2.0 using Homebrew on OS X 10.11.4.
I'm compiling assembly at runtime and link it via adding to new domain. I use it and then unload domain. But when I try to compile again during same run I can't get access to that assembly because it currently in use.
Here are some of my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary2
{
public interface IExtension
{
String GetExtensionName();
}
}
My assembly
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary2;
namespace ClassLibrary1
{
public class Extension1 : MarshalByRefObject, IExtension
{
public Extension1()
{
}
public string GetExtensionName()
{
return "Extension 1 from " + AppDomain.CurrentDomain.FriendlyName;
}
}
}
And the app that uses it
namespace ConsoleApplication7
{
class Program
{
static IEnumerable<IExtension> extensions;
static void Main(string[] args)
{
// Create app domain
AppDomain domain = CreateDomain(Directory.GetCurrentDirectory());
try
{
// Get extensions
extensions = EnumerateExtensions(domain);
foreach (IExtension extension in extensions)
// Execute extension method in separate domain.
Console.WriteLine(extension.GetExtensionName());
// Unload domain
UnloadDomain(domain);
}
finally
{
domain = null;
GC.Collect(2);
extensions = null;
}
Console.ReadKey();
}
private static IEnumerable<IExtension> EnumerateExtensions(AppDomain domain)
{
IEnumerable<string> fileNames = Directory.EnumerateFiles(domain.BaseDirectory, "*.dll");
if (fileNames != null)
{
foreach (string assemblyFileName in fileNames)
{
foreach (string typeName in GetTypes(assemblyFileName, typeof(IExtension), domain))
{
System.Runtime.Remoting.ObjectHandle handle;
try
{
handle = domain.CreateInstanceFrom(assemblyFileName, typeName);
}
catch (MissingMethodException)
{
continue;
}
object obj = handle.Unwrap();
IExtension extension = (IExtension)obj;
yield return extension;
}
}
}
}
private static IEnumerable<string> GetTypes(string assemblyFileName, Type interfaceFilter, AppDomain domain)
{
Assembly asm = domain.Load(AssemblyName.GetAssemblyName(assemblyFileName));
Type[] types = asm.GetTypes();
foreach (Type type in types)
{
if (type.GetInterface(interfaceFilter.Name) != null)
{
yield return type.FullName;
}
}
}
static AppDomain CreateDomain(string path)
{
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = path;
return AppDomain.CreateDomain("Temporary domain", null, setup);
}
static void UnloadDomain(AppDomain domain)
{
AppDomain.Unload(domain);
}
}
}
So in Main() during Console.ReadKey(); assembly still locked and I can't get access to it (can't delete it via Windows for example).
Is there way to solve this?
I think the domain.Load is hooking up the assembly to your program try to the load inside the Extension1 try to move the GetTypes to the class Extension1
I don't remember but I think domain.Load just runs Assembly.LoadFrom( and that's what is connecting your application the the DLL.
Ok, I solved this problem. I used shadow copy, just configured shadow copy in that other domain and it worked for me.
I have a Windows Phone 8.1 Class Library that I want to later add as a reference to a Windows Phone 8.1 App project.
This ClassLibrary should be responsible for creating and managing its own database. I tried creating a new SQLiteConnection in my ClassLibrary, but it throws the following error: A first chance exception of type 'System.InvalidOperationException' occurred in SQLitePCL.DLL however, if I do the same in my MainApp everything works fine.
So, is it possible to create a SQLite database in a ClassLibrary that's responsible for creating and managing it without any support from the MainApp.
I have a project in it where the SQLite library is in a class library and then I use another class library for the communication between my app and the SQLite library
Class library: SQLite.Library
Make a new class library (in my case I named it SQLite.Library)
Right click > Manage NuGet packages > sqlite-net (https://www.nuget.org/packages/sqlite-net/1.0.8)
After adding this NuGet package you see that your class library has 2 new classes: SQLite.cs and SQLiteAsync.cs.
Also there is a known problem with SQLite and threading (NullReferenceException when page Loads), you can fix it by adding a lock in the method TableMapping GetMapping in SQLite.cs:
public TableMapping GetMapping(Type type, CreateFlags createFlags = CreateFlags.None)
{
if (_mappings == null) {
_mappings = new Dictionary<string, TableMapping> ();
}
lock (_mappings)
{
TableMapping map;
if (!_mappings.TryGetValue(type.FullName, out map))
{
map = new TableMapping(type, createFlags);
_mappings[type.FullName] = map;
}
return map;
}
}
Class library: Solutionname.Lib
Make a new class library (in my case I named it Solutionname.Lib)
Right click > Add Reference > Solution > SQLite.Library (the class library u just made)
After the reference is set u can use the SQLite library in this class library.
In my project I tried to split my code a bit so I started with making a class named DatabaseHelper.cs:
public class DatabaseHelper
{
private String DB_NAME = "DATABASENAME.db";
public SQLiteAsyncConnection Conn { get; set; }
public DatabaseHelper()
{
Conn = new SQLiteAsyncConnection(DB_NAME);
this.InitDb();
}
public async void InitDb()
{
// Create Db if not exist
bool dbExist = await CheckDbAsync();
if (!dbExist)
{
await CreateDatabaseAsync();
}
}
public async Task<bool> CheckDbAsync()
{
bool dbExist = true;
try
{
StorageFile sf = await ApplicationData.Current.LocalFolder.GetFileAsync(DB_NAME);
}
catch (Exception)
{
dbExist = false;
}
return dbExist;
}
private async Task CreateDatabaseAsync()
{
//add tables here
//example: await Conn.CreateTableAsync<DbComment>();
}
}
After the creation of the DatabaseHelper class u can start by making a datasource class for each table in your database.
In my case i have a CommentDataSource.cs:
public class CommentDataSource
{
private DatabaseHelper db;
public CommentDataSource(DatabaseHelper databaseHelper)
{
this.db = databaseHelper;
}
public async Task<long> AddComment(String vat, String comment)
{
long id = 0;
DateTime date = DateTime.Now;
DbComment dbc = new DbComment(vat, comment, date);
await db.Conn.InsertAsync(dbc);
DbComment insertDbc = await db.Conn.Table<DbComment>().ElementAtAsync(await db.Conn.Table<DbComment>().CountAsync() - 1);
if (insertDbc != null)
{
id = insertDbc.Id;
}
return id;
}
public async void RemoveComment(long idComment)
{
DbComment comment = await db.Conn.Table<DbComment>().Where(c => c.Id == idComment).FirstOrDefaultAsync();
if (comment != null)
{
await db.Conn.DeleteAsync(comment);
}
}
public async Task<List<DbComment>> FetchAllComments(String vat)
{
return await db.Conn.Table<DbComment>().Where(x => x.VAT == vat).ToListAsync();
}
}
As you can see all the datasources that u will add will make use of the same databasehelper.
Use the Solutionname.Lib in your app
Right click > Add Reference > Solution > SQLite.Library (the class library u just made)
Right click > Add Reference > Solution > Solutionname.Lib
You still need to add a reference to your sqlite lib otherwise you will get errors.
Now you can start using your datasource classes, like u can see here:
private DatabaseHelper db = new DatabaseHelper();
private CommentDataSource commentDataSource;
public MainPage()
{
this.InitializeComponent();
commentDataSource = new CommentDataSource(db);
}
Now is every method of the CommentsDataSource available in your app.
Hope this help u a bit!
try this
public async Task<bool> CheckDbAsync(string dbName)
{
bool dbExist = true;
try
{
StorageFile sf = await ApplicationData.Current.LocalFolder.GetFileAsync(dbName);
}
catch (Exception)
{
dbExist = false;
}
return dbExist;
}
public async Task CreateDatabaseAsync(string dbName)
{
SQLiteAsyncConnection con = new SQLiteAsyncConnection(dbName);
await con.CreateTableAsync<ChatClass>();
// await con.CreateTableAsync<RecentChatManageClass>();
await con.CreateTableAsync<PurchasedGift>();
// await con.CreateTableAsync<AttandanceManagement>();
}
and use like this
DataBaseOperation databaseoperation = new DataBaseOperation();
bool existDb = await databaseoperation.CheckDbAsync("sample.db"); // Check Database created or not
if (!existDb)
{
await databaseoperation.CreateDatabaseAsync("sample.db"); // Create Database
}