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. Determining type compatiblity

Determining type compatiblity

Scheduled Pinned Locked Moved C#
helpcsharpdatabasexmlquestion
4 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.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    Hi folks! I've coded myself into another corner... again! I need a guru. This time, I'm writing code that checks that the parameter list for a call to a method is valid. Why? Because I am dynamically calling various methods with parameters read from an XML file. Why? Don't ask... looooong story. Breifly, a call to my routine might look like

    StandardTest.PerformDALvsDBTest(
    "DALRoutines", // the class that will contain the method to call
    "InsertCustomer", // the method to be called
    new object[] {
    DateTime.Parse(parm0),
    parm1,
    Int32.Parse(parm2)
    }
    );

    Okay. So my routine goes through a bunch of checks and finally obtains a reference to the method to be called (MethodInfo.methodRef). The call to the method will be made using

    methodRef.Invoke(null, myBindings, null, Params, null);

    But first, I must make sure that all the items in the object[] Params are of the right type. To do that I loop through methodRef.GetParameters() and check each one against each Params[index].GetType(). If there are the correct number of parameters, and they are all of the correct type, etc, then I will perform the Invoke(), otherwise, I throw an error indicating the method name, the parameter, the expected type and the type that was used: e.g. "The System.Type for parameter #0 in the parameter list for the method [DALRoutines.InsertCustomer] is System.DateTime; System.TimeSpan was expected". So, here is my problem: I have a method that expects a parameter to be an Int32. However, when the value is passed in the object[] Params, it seems to be changed to Int16 (even if I specifically indicate that it is Int32 as above - I assume that it is because it is a small value). When my routine starts comparing parameter types it spits out an error saying it was passed an Int16 when it expects an Int32. The kicker is that the call to Invoke() would still work if it was made - .NET does not seem to mind and handles the conversion inherently. But I can't find a way to tell that it would work - all I can see is that the two types are different. I have tried investigating different ways to tell if the types are compatible (including Type.IsSubclassOf() and Type.IsAssignableFrom()) but I can't seem to find anything that will let me know that an Int16 can be used as the val

    P L 2 Replies Last reply
    0
    • C Clive D Pottinger

      Hi folks! I've coded myself into another corner... again! I need a guru. This time, I'm writing code that checks that the parameter list for a call to a method is valid. Why? Because I am dynamically calling various methods with parameters read from an XML file. Why? Don't ask... looooong story. Breifly, a call to my routine might look like

      StandardTest.PerformDALvsDBTest(
      "DALRoutines", // the class that will contain the method to call
      "InsertCustomer", // the method to be called
      new object[] {
      DateTime.Parse(parm0),
      parm1,
      Int32.Parse(parm2)
      }
      );

      Okay. So my routine goes through a bunch of checks and finally obtains a reference to the method to be called (MethodInfo.methodRef). The call to the method will be made using

      methodRef.Invoke(null, myBindings, null, Params, null);

      But first, I must make sure that all the items in the object[] Params are of the right type. To do that I loop through methodRef.GetParameters() and check each one against each Params[index].GetType(). If there are the correct number of parameters, and they are all of the correct type, etc, then I will perform the Invoke(), otherwise, I throw an error indicating the method name, the parameter, the expected type and the type that was used: e.g. "The System.Type for parameter #0 in the parameter list for the method [DALRoutines.InsertCustomer] is System.DateTime; System.TimeSpan was expected". So, here is my problem: I have a method that expects a parameter to be an Int32. However, when the value is passed in the object[] Params, it seems to be changed to Int16 (even if I specifically indicate that it is Int32 as above - I assume that it is because it is a small value). When my routine starts comparing parameter types it spits out an error saying it was passed an Int16 when it expects an Int32. The kicker is that the call to Invoke() would still work if it was made - .NET does not seem to mind and handles the conversion inherently. But I can't find a way to tell that it would work - all I can see is that the two types are different. I have tried investigating different ways to tell if the types are compatible (including Type.IsSubclassOf() and Type.IsAssignableFrom()) but I can't seem to find anything that will let me know that an Int16 can be used as the val

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      You could always try to perform a cast on it. It's not the most efficient method, but I figure that's not what you're worried about - what with the reflection and all.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      1 Reply Last reply
      0
      • C Clive D Pottinger

        Hi folks! I've coded myself into another corner... again! I need a guru. This time, I'm writing code that checks that the parameter list for a call to a method is valid. Why? Because I am dynamically calling various methods with parameters read from an XML file. Why? Don't ask... looooong story. Breifly, a call to my routine might look like

        StandardTest.PerformDALvsDBTest(
        "DALRoutines", // the class that will contain the method to call
        "InsertCustomer", // the method to be called
        new object[] {
        DateTime.Parse(parm0),
        parm1,
        Int32.Parse(parm2)
        }
        );

        Okay. So my routine goes through a bunch of checks and finally obtains a reference to the method to be called (MethodInfo.methodRef). The call to the method will be made using

        methodRef.Invoke(null, myBindings, null, Params, null);

        But first, I must make sure that all the items in the object[] Params are of the right type. To do that I loop through methodRef.GetParameters() and check each one against each Params[index].GetType(). If there are the correct number of parameters, and they are all of the correct type, etc, then I will perform the Invoke(), otherwise, I throw an error indicating the method name, the parameter, the expected type and the type that was used: e.g. "The System.Type for parameter #0 in the parameter list for the method [DALRoutines.InsertCustomer] is System.DateTime; System.TimeSpan was expected". So, here is my problem: I have a method that expects a parameter to be an Int32. However, when the value is passed in the object[] Params, it seems to be changed to Int16 (even if I specifically indicate that it is Int32 as above - I assume that it is because it is a small value). When my routine starts comparing parameter types it spits out an error saying it was passed an Int16 when it expects an Int32. The kicker is that the call to Invoke() would still work if it was made - .NET does not seem to mind and handles the conversion inherently. But I can't find a way to tell that it would work - all I can see is that the two types are different. I have tried investigating different ways to tell if the types are compatible (including Type.IsSubclassOf() and Type.IsAssignableFrom()) but I can't seem to find anything that will let me know that an Int16 can be used as the val

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

        Would something like this be okay?

        	public static bool ImplicitCastAllowed(Type type, object value)
        	{
        		try
        		{
        			Convert.ChangeType(value, type);
        			return true;
        		}
        		catch
        		{
        			return false;
        		}
        	}
        

        regards

        C 1 Reply Last reply
        0
        • L Lost User

          Would something like this be okay?

          	public static bool ImplicitCastAllowed(Type type, object value)
          	{
          		try
          		{
          			Convert.ChangeType(value, type);
          			return true;
          		}
          		catch
          		{
          			return false;
          		}
          	}
          

          regards

          C Offline
          C Offline
          Clive D Pottinger
          wrote on last edited by
          #4

          Thanks guys - but I found the cause of the problem... P.E.B.K.A.C.! I was actually rewriting the routine to use Convert() (thanks Greeeg), when I noticed that I my error message was printing out the wrong info - instead of saying that the parm was "of type passedParmType; requiredParmType was expected", it said it was of "type requiredParmType; passedParmType was expected"! That led to finding other places where I had mixed up the two references (due to an ambiguous naming convention). Once I fixed that I found that my code was actually working correctly and warning me that I was trying to use an Int32 as a value to an Int16 parameter. :doh: Perhaps I should consider a change of career. I hear that Air Traffic Control is less stressful... and apparently the FAA is much more lenient about minor errors than IT managers.

          Clive Pottinger Victoria, BC

          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