mercredi 24 février 2021

Use reflection in .net standard referenced from .net framework

I made a library in .net standard 2.0. It's a library that uses a factory to create WebService objects that have common methods since the WebServices are different versions. I opted to use reflection to invoke the method. It works great in my unit tests. I ran into a problem when I tried to actually use the library in my .Net Framework 4.8 project. When you do that you get this exception:

PlatformNotSupportedException: This operation is not supported on .NET Standard as Reflection.Emit is not available.

It's thrown from the Invoke method.

        protected virtual async Task<TReturn> CallWebMethod<TReturn>(string methodName, params object[] paramaters) {
            MethodInfo info = WebServiceInstance.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);            
            return await (Task<TReturn>)info.Invoke(WebServiceInstance, paramaters);            
        }

I've tried adding the Nuget package for System.Reflection.Emit to the library and .Net Framework project to see if that makes it happy. It doesn't.

I'm at a loss, is there any way to use a .Net Standard library that implements some reflection from a .Net Framework project?





Aucun commentaire:

Enregistrer un commentaire