Convert int to nullable int in JavaScript for use with .net method

Jul 7, 2011 at 2:22 PM

I'm trying to pass a JavaScript integer to a .net method which requires it to be nullable.  Is there a way to do this natively from within a script (preferably without helper functions in the host app)?  If so, do you have an example?

Jul 8, 2011 at 1:52 PM

I took a crack at it this morning...and I'm thinking the answer is 'no,' although maybe someone with more familiarity with Jint would have a different answer.  The reason the answer is no is that it appears Jint always returns an object when you call javascript.  That means that you always end up needing to do some kind of conversion from object to whatever your target type is.  I could get something like this to work, but it seems to be the minimum amount of code needed:

object returned = myModule.Engine.Run("return someFunctionThatReturnsNullOrInt();");
if(returned == null){
    myNullable = (int?)returned;
}else{
    myNullable = Convert.ToInt32(returned);
}

I'm thinking some basic helper methods might be a nice addition to Jint here.  I don't know know how feasible it would be, but a JintEngine.Run<T>() would be really handy.  I'm not sure how much it would take to accomplish without using reflection, though...or if one could be written to cover all cases.  I may attempt it.

Jul 8, 2011 at 2:25 PM

Alright, so after a little surfing, I've found a generic conversion function that appears like it will work in most, if not all, situations.  The basic code can be found here at http://blogs.msdn.com/b/jongallant/archive/2006/06/19/637023.aspx .  There doesn't appear to be any kind of license attached, so I don't know what the implications of that might be.  The code also appears to have a bug in it, but I've got a fixed version:

private static T ConvertTo<T>(object value)
{
        //added by JediMindtrick
	bool isNullable = false;
	
        Type t = typeof(T);
	
        // do we have a nullable type?
        if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
        {
            NullableConverter nc = new NullableConverter(t);
            t = nc.UnderlyingType;
            //added by JediMindtrick
            isNullable = true;
       }
	
        if (t.IsEnum) // if enum use parse
            return (T)Enum.Parse(t, value.ToString(), false);
        else
        {
            // if we have a custom type converter then use it
            TypeConverter td = TypeDescriptor.GetConverter(t);

            //added by JediMindtrick
           if(isNullable && value == null){
		return (T)value;
	    }
            else if (td.CanConvertFrom(value.GetType()))
            {
                return (T)td.ConvertFrom(value);
            }
            else // otherwise use the changetype
                return (T)Convert.ChangeType(value, t);
        }
}
So, depending on the licensing issue (if it exists....) I think this could be used to build a JintEngine.Run<T>().

Jul 10, 2011 at 10:56 AM

Thanks for the tips.  In the end, we decided to use a pass through function in the host app to deal with the problem and avoid modifying the original class or Jint in case we created other issues.  Your suggested extension would be useful, however.