Search code examples
c#azure.net-corereflectionazure-service-fabric

Cannot make a Generic Class of Type System.Fabric.FabricServiceNotFoundException when using c# reflection


I have the following function that takes in an error and creates a class instance of ErrorType<T>

 private dynamic GenerateDynamicErrorHandler(Exception ex)
    {
        try
        {
            string typeName = ex.GetType().FullName;
            Type typeArgument = Type.GetType(typeName);
            Type genericClass = typeof(ErrorType<>);

            Type constructedClass = genericClass.MakeGenericType(typeArgument);

            dynamic created = Activator.CreateInstance(constructedClass);
            return created;
        }
        catch
        {
            throw new ReflectionException(ex.GetType().FullName);
        }
    }

Which then returns a dynamic object in the following code snippet:

 try
        {
            dynamic c = GenerateDynamicErrorHandler(ex);

            if (c.IsError())
            {
                return c.GenericResponse<ErrorDetails,AppendingErrors>(isDebug,
                    isDefaultConverter,
                    context, 
                    _errorDetailResponseFactory.CreateDefaultError(ex.Message,ex.InnerException,context.Response.StatusCode),
                    new AppendingErrors
                    {
                        Title = c.Get().Title,
                        ResultCode = c.Get().ResultCode,
                        StackTrace = ex.StackTrace

                    });
            }
            return HandleError.WithDefaultJsonConverter<ErrorDetails>(context, 
                _errorDetailResponseFactory.CreateDefaultError(ex.Message,ex.InnerException,context.Response.StatusCode));
        }
        catch(ReflectionException re)
        {
            return HandleError.WithGenericApendingJsonConverter<ErrorDetails, object>(context,
            _errorDetailResponseFactory.CreateDefaultError(ex.Message, ex.InnerException, context.Response.StatusCode), 
            new {
                reflectionError = re.Message
            });
        }

This works for all Exceptions except for System.Fabric.FabricServiceNotFoundException and I dont understand or have any clue as to why. If anyone could assist with this, it would be greatly appreciated


Solution

  • So thanks to @Flydog57 the problem was actually getting the name of the type and then trying to make a type from that name. The simple fix was to, as said in the comment, just use the type of the exception passed in

    private dynamic GenerateDynamicErrorHandler(Exception ex)
        {
            try
            {
                Type typeArgument = ex.GetType();
                Type genericClass = typeof(ErrorType<>);
    
                Type constructedClass = genericClass.MakeGenericType(typeArgument);
    
                dynamic created = Activator.CreateInstance(constructedClass);
                return created;
            }
            catch(Exception e)
            {
                throw new ReflectionException(ex.GetType().FullName);
            }
        }