5

Create a class dynamically using System.Reflection.Emit in UWP. in UWP AppDomain.CurrentDomain.DefineDynamicAssembly is missing. Please suggest any solution.

SINU S
  • 59
  • 3
  • What are you going to use the created the class for? – Peter Torr - MSFT Jun 16 '16 at 11:33
  • Both [AssemblyBuilder](https://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilder.aspx) as well as [TypeBuilder](https://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.aspx) are available for the UWP since Windows 10. What specifically are you having issues with? – IInspectable Jun 16 '16 at 12:01
  • AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run) is giving error in uwp. please suggest an alternative approach to get AppDomain or how to initialize AssemblyBuilder in UWP – SINU S Jun 17 '16 at 04:37
  • 1
    Makes you wonder, why I even bothered giving you the links. The documentation for [TypeBuilder](https://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.aspx) has everything you are asking for. The same goes for [AssemblyBuilder](https://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilder.aspx). Can you not read, or are you just too lazy? – IInspectable Jun 18 '16 at 18:19
  • sorry for asking again my above question is very clear that how to initialize AssemblyBuilder in order to initialize AssemblyBuilder in UWP we need "AppDomain.CurrentDomain" which is missing in UWP or not able to resolve. if able to resolve the error which assembly that I need to refer.I build all the code by looking documentation already ony problem is not able to resolve "AppDomain" if this problem resolve all my code will work. I need to create my model class based on my data from WebApi that is the requirement. – SINU S Jun 20 '16 at 10:38

2 Answers2

3

You need to include the Nuget package for System.Reflection.Emit in your project. They've been breaking out parts of the API into separate packages.

Then you can use the AssemblyBuilder.DefineDynamicAssembly method to create an assembly in memory in a UWP project.

But this won't work with the .NET Native Toolchain, which means you probably won't be able to submit to the store:

https://blogs.windows.com/buildingapps/2015/08/20/net-native-what-it-means-for-universal-windows-platform-uwp-developers/#8t4LmYa5JGBQXsRk.97

https://learn.microsoft.com/en-us/windows/uwp/packaging/packaging-uwp-apps

Michael Hawker - MSFT
  • 1,572
  • 12
  • 18
1

From what I've read through the Windows 10 API and from trying to code this myself, AppDomain is not available at all. Although the TypeBuilder and AssemblyBuilder API show that it's supported in Windows 10, this seems to be false. It doesn't actually exist within the UWP version of System.Reflection and System.Reflection.Emit. Neither does AppDomain.

Windows 10 Supported Reflection Types

snickler
  • 94
  • 3