Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

Invoke a generic method with generic arguments defined in a generic class in C#

here is my code:

  class Program1
    {
        private static void Main(string[] args)
        {
            //MyClass myClass = new MyClass();
            //GetIdRequest getIdRequest = new GetIdRequest();
            //GetIdReponse getIdReponse= myClass.DoRequest(getIdRequest);
            //Console.WriteLine($"getIdReponse.Msg:" + getIdReponse.Msg + ",getIdReponse.Data:" + getIdReponse.Data);

            Type myClassType = typeof(MyClass);
            Object myClassTypeInstance = System.Activator.CreateInstance(myClassType);
            Type unboundGenericType = typeof(MyRequest<>);
            Type boundGenericType = unboundGenericType.MakeGenericType(typeof(GetIdRequest));

            Type getIdRequestType = typeof(GetIdRequest);
            Object getIdRequest = System.Activator.CreateInstance(getIdRequestType);


            MethodInfo methodInfo = myClassType.GetMethod("DoRequest", BindingFlags.Public| BindingFlags.Instance, null, new Type[] { boundGenericType }, null);


            object res = methodInfo.Invoke(myClassTypeInstance, new object[] { getIdRequest });

            Console.ReadKey();
        }

    }
    public class MyClass
    {
        public T DoRequest<T>(MyRequest<T> obj) where T : MyReponse
        {
            T res = Activator.CreateInstance<T>();
            return res;
        }
    }

    public class GetIdRequest: MyRequest<GetIdReponse>
    {
    }

    public class GetIdReponse: MyReponse
    {
        public string Data { set; get; } = "data";
    }

    public interface MyRequest<T> where T: MyReponse
    {

    }

    public class MyReponse
    {
        public string Msg { set; get; } = "res";
    }

i want to Invoke a generic method with generic arguments defined in a generic class by runtime but i get the Exception

"System.ArgumentException :““ TestPlatformApi.MyRequest `Genericarguments [0] on 1 [t] "“ TestPlatformApi.GetIdRequest ”Violation of constraint on type't'"

question from:https://stackoverflow.com/questions/65894905/invoke-a-generic-method-with-generic-arguments-defined-in-a-generic-class-in-c-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. As Charlieface commented, you should remove contraint on MyRequest.
  2. From your code, I could not get your design enough. So, I just show some code under suppose.
 public class MyClass<T> where T : MsgConstraint
    {
        private IList<T> middlewares = new List<T>();

        public void Register(T m)
        {
            middlewares.Add(m);
        }

        public T DoRequest(Type type)
        {
            return middlewares.Where(st => st.GetType() == type).FirstOrDefault();
        }
    }

    public interface MsgConstraint
    {
        public string Msg { set; get; }
    }

    public class HttpMsg : MsgConstraint
    {
        public string Msg { get; set; } = "HTTPMSG";
    }

    public class RpcMsg : MsgConstraint
    {
        public string Msg { get; set; } = "RPCMSG";
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyClass<MsgConstraint> myClass = new MyClass<MsgConstraint>();
            myClass.Register(new HttpMsg());
            myClass.Register(new RpcMsg());
            MsgConstraint http = myClass.DoRequest(typeof(HttpMsg));
            Console.WriteLine(http.Msg);
            MsgConstraint rpc = myClass.DoRequest(typeof(RpcMsg));
            Console.WriteLine(rpc.Msg);
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...