Search code examples
c#.netblazorblazor-server-side

Difference between executing source code written in publish and debug


I have written a class whose job is to return the maximum and minimum value of any data type This class works fine when debugging the project, but when we publish and run it and enter the part where we use this class, the following error occurs.

crit: microsoft.aspnetcore.components.webassembly.rendering.webassemblyrenderer[100] unhandled exception rendering component: error: no element is currently associated with component 250 error: no element is currently associated with component 250

public static class MinMaxDefaultValue<T>
{
    public static T? Min { get; }
    public static T? Max { get; }
    public static T? Step { get; }
    public static T? Default { get; }
    public static Type Type { get; } = typeof(T);
    static MinMaxDefaultValue()
    {
        Default = default;
        if (Default is null) return;

        var interfaces = Type.GetInterfaces();
        if (!interfaces.Any(t => t.GetGenericTypeDefinition() == typeof(INumber<>))) return;

        Step = (T)Convert.ChangeType(1, Type);
        Min = GetValue("MinValue");
        Max = GetValue("MaxValue");
    }
    public static T? GetValue(string fieldName)
    {
        var fieldInfo = Type.GetField(fieldName);
        if (fieldInfo is null) return default;
        return (T?)fieldInfo.GetValue(null);
    }
}

Solution

  • After reviewing the source code and taking about 20 publications and testing them, I realized that the problem was due to the following code breaking.

        t => t.GetGenericTypeDefinition() == typeof(INumber<>)
    

    The above code snippet seems to be fine, but when we publish it and test the application in a real environment, an error occurs. The reason is that in .NET, the GetGenericTypeDefinition method can only be used for generic data types and gives an error in other cases.

    I changed the code as follows and the problem was solved:

        t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(INumber<>)
    

    my class after change :

    public static class MinMaxDefaultValue<T>
    {
        public static T? Min { get; }
        public static T? Max { get; }
        public static T? Step { get; }
        public static T? Default { get; }
        public static Type Type { get; } = typeof(T);
        static MinMaxDefaultValue()
        {
            Default = default;
            if (Default is null) return;
    
            var interfaces = Type.GetInterfaces();
            if (!interfaces.Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(INumber<>))) return;
    
            Step = (T)Convert.ChangeType(1, Type);
            Min = GetValue("MinValue");
            Max = GetValue("MaxValue");
        }
        public static T? GetValue(string fieldName)
        {
            var fieldInfo = Type.GetField(fieldName);
            if (fieldInfo is null) return default;
            return (T?)fieldInfo.GetValue(null);
        }
    }