简体   繁体   中英

Generic method/class custom implementation for specified type

Is there any way to make this code compile or some other alternative. This implementation is only a example.

namespace ConsoleApp3
{
    using System;

    public static class Temp<T>
        where T : struct
    {
        public static T DoSomething()
            => throw new NotImplementedException(); // common implementation

        public static int DoSomething()
            => 10; // custom implementation for specified type
    }
}

From reading the comments - if the intent is to override methods then the methods can't be static. The sample code where the "common" implementation throws a NotImplementedException suggests that you don't want there to be a base implementation, only overridden implementations. In that case you could do this:

public abstract class Temp<T>
    where T : struct
{
    public abstract T GetDefault();
}

Because it's abstract you can't create an instance of it. You have to create inherited classes.

public class IntTemp : Temp<int>
{
    public override int GetDefault()
    {
        // whatever your implmementation is
    }
}

I found 2 alternatives.

|               Method |     Mean |    Error |   StdDev | Allocated |
|--------------------- |---------:|---------:|---------:|----------:|
|     Benchmark_Single | 306.8 ns | 2.867 ns | 2.682 ns |       0 B |
| Benchmark_Reflection | 581.8 ns | 3.062 ns | 2.864 ns |       0 B |

-

namespace ConsoleApp3
{
    using System;
    using System.Linq.Expressions;
    using System.Reflection;

    public static class TempSingle<T>
        where T : struct
    {
        public static T DoCustom()
        {
            if (typeof(T) == typeof(int))
            {
                return (T)((object)int.MinValue);
            }
            else if (typeof(T) == typeof(ushort))
            {
                return (T)((object)ushort.MinValue);
            }

            return default;
        }
    }

    public static class TempReflection<T>
        where T : struct
    {
        public readonly static Func<T> Factory = GetFactory();

        private static int GetDefaultInt32()
        {
            return int.MinValue;
        }

        private static ushort GetDefaultUInt16()
        {
            return ushort.MinValue;
        }

        private static Func<T> GetFactory()
        {
            Type type = typeof(T);

            MethodInfo method = typeof(TempReflection<T>)
                .GetMethod($"GetDefault{type.Name}", BindingFlags.Static | BindingFlags.NonPublic);

            return Expression
                .Lambda<Func<T>>(Expression.Call(method))
                .Compile();
        }

        public static T DoCustom()
        {
            return default;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM