Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. About Typed parameters

About Typed parameters

Scheduled Pinned Locked Moved C#
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ika2
    wrote on last edited by
    #1

    Hi everybody, I have this abstract method:

    public static T DecodeData<T>(byte[] data, string encRules)

    But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.

        public static Type GetDownloadType (int id)
        {
    	    switch (id)
            {
    	        case DOWNDIRECT: return typeof(DownDirect);
    	        case DOWNOTHER: return typeof(DownOther);
    	        case DOWNHEHE: return typeof(DownHehe);
    	        default: return null;
    	    }
        }
    

    But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced

    D L 3 Replies Last reply
    0
    • I ika2

      Hi everybody, I have this abstract method:

      public static T DecodeData<T>(byte[] data, string encRules)

      But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.

          public static Type GetDownloadType (int id)
          {
      	    switch (id)
              {
      	        case DOWNDIRECT: return typeof(DownDirect);
      	        case DOWNOTHER: return typeof(DownOther);
      	        case DOWNHEHE: return typeof(DownHehe);
      	        default: return null;
      	    }
          }
      

      But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      T needs to be resolvable at compile time i.e before runtime when you have access to the type via typeof

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      1 Reply Last reply
      0
      • I ika2

        Hi everybody, I have this abstract method:

        public static T DecodeData<T>(byte[] data, string encRules)

        But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.

            public static Type GetDownloadType (int id)
            {
        	    switch (id)
                {
        	        case DOWNDIRECT: return typeof(DownDirect);
        	        case DOWNOTHER: return typeof(DownOther);
        	        case DOWNHEHE: return typeof(DownHehe);
        	        default: return null;
        	    }
            }
        

        But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The type things used in generics are not instances of Type But of course you could put the calls to DecodeData in that switch (and some code that uses its result too, perhaps, otherwise you may get a similar problem with the result type)

        1 Reply Last reply
        0
        • I ika2

          Hi everybody, I have this abstract method:

          public static T DecodeData<T>(byte[] data, string encRules)

          But the type T must be defined during runtime. I mean... I have other method like: GetDownloadType(id); Depend on the id, I should pass one or another parameter to DecodeData. But I dont know how.

              public static Type GetDownloadType (int id)
              {
          	    switch (id)
                  {
          	        case DOWNDIRECT: return typeof(DownDirect);
          	        case DOWNOTHER: return typeof(DownOther);
          	        case DOWNHEHE: return typeof(DownHehe);
          	        default: return null;
          	    }
              }
          

          But when I try to use the Type result of this method to pass it to DecodeData... I cant. How should I do this? Thanks in advanced

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          If you really need to invoke a generic method you could use something like this:

          public static class Test
          {
          public static T GenericDecodeData<T>(byte[] data, string encRules)
          {
          // ...
          return default(T);
          }

          public static Type GetDownloadType(int id)
          {
              switch (id)
              {
                  case DOWNDIRECT: return typeof(DownDirect);
                  case DOWNOTHER: return typeof(DownOther);
                  case DOWNHEHE: return typeof(DownHehe);
                  default: return null;
              }
          }
          public static object DecodeData(Type type, byte\[\] data, string encRules)
          {
              MethodInfo methodInfo = typeof(Test).GetMethod("GenericDecodeData").MakeGenericMethod(
                  new Type\[\] { type });
              return methodInfo.Invoke(null, new object\[\] { data, encRules });
          }
          

          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          I 1 Reply Last reply
          0
          • D DaveyM69

            If you really need to invoke a generic method you could use something like this:

            public static class Test
            {
            public static T GenericDecodeData<T>(byte[] data, string encRules)
            {
            // ...
            return default(T);
            }

            public static Type GetDownloadType(int id)
            {
                switch (id)
                {
                    case DOWNDIRECT: return typeof(DownDirect);
                    case DOWNOTHER: return typeof(DownOther);
                    case DOWNHEHE: return typeof(DownHehe);
                    default: return null;
                }
            }
            public static object DecodeData(Type type, byte\[\] data, string encRules)
            {
                MethodInfo methodInfo = typeof(Test).GetMethod("GenericDecodeData").MakeGenericMethod(
                    new Type\[\] { type });
                return methodInfo.Invoke(null, new object\[\] { data, encRules });
            }
            

            }

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            I Offline
            I Offline
            ika2
            wrote on last edited by
            #5

            Thanks both. I think that your solution, Dave, could do the trick!!! Thankssss!! Kind regards

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups