Reputation: 4165
I am trying to create Mobile App Using Xamarin.Forms (Portable Class Library), On the following Code, I can not use `GetExecutingAssembly:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Reflection;
using System.IO;
using PCLStorage;
namespace MnakbAlshaba
{
public class BookPage : ContentPage
{
//
private void SetBookPagesRows()
{
var assembly = typeof(BookPage).GetTypeInfo().Assembly.GetExecutingAssembly();//error
var resourceName = "MyCompany.MyProduct.MyFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
}
}
}
I Get the following Error:
'System.Reflection.Assembly' does not contain a definition for 'GetExecutingAssembly' and no extension method 'GetExecutingAssembly' accepting a first argument of type 'System.Reflection.Assembly' could be found (are you missing a using directive or an assembly reference?) C:...
What should I do?
Upvotes: 3
Views: 5336
Reputation: 3897
Make sure you haven't created your own Assembly class somewhere.
If it exists, the compiler will look for the GetExecutingAssembly()
method in there.
I accidentally did this which is why I'm here.
You can fix it by either renaming your Assembly class, or adding the specific location which is usually left out thanks to a using statement.
System.Reflection.Assembly.GetExecutingAssembly()
or
System.Reflection.Assembly.GetAssembly(type)
Upvotes: 2
Reputation: 24690
It is crucial here to understand that the Portable Class Library / PLC as a platform profile doesn't exist. The running application will not incur the same restrictions as the compiler does when compiling your PLC project.
Here is one way to break through the barrier:
using System;
...
try {
var getExecutingAssembly = typeof(Assembly).GetRuntimeMethods()
.Where(m => m.Name.Equals("GetExecutingAssembly"))
.FirstOrDefault();
var assemblies = getExecutingAssembly.Invoke(null, null);
} catch(Exception exc){
... try something else
} finally{
... time for some alternative
}
This approach will only yield you the accessible assemblies within the sandboxed assembly environment. But it gives you a starting point on how to access the "stuff" you are not supposed to.
Upvotes: 1