How can we help you today? How can we help you today?

Bug when reflecting public method parameters of type Guid

Product: Smart Assembly Professional 6.8.0.121

This bug came up while using Microsofts WebAPI framework. It occurs when an interface method has either a Nullable<Guid> or Guid = default(Guid) parameter. Here is a console app to reproduce the issue:

using System;
using System.Reflection;

namespace ObfustactionTest
&#123;
	&#91;Obfuscation&#40;Exclude=true&#41;&#93;
	class Program
	&#123;
		static void Main&#40;&#41;
		&#123;
			var method = new Action&lt;Guid&gt;&#40;MyMethod&#41;;
			Console.Write&#40;"method:"&#41;;
			Console.WriteLine&#40;method&#41;;

			var param = method.Method.GetParameters&#40;&#41;&#91;0&#93;;
			Console.Write&#40;"param:"&#41;;
			Console.WriteLine&#40;param&#41;;

			Console.Write&#40;"RuntimeParameterInfo.DefaultValue:"&#41;;
			Console.WriteLine&#40;param.DefaultValue ?? "&#40;null&#41;"&#41;;

			Console.WriteLine&#40;"Success"&#41;;
			Console.ReadLine&#40;&#41;;
		&#125;

		public static void MyMethod&#40;Guid arg1 = default&#40;Guid&#41;&#41;
		&#123;
			Console.WriteLine&#40;arg1&#41;;
		&#125;
	&#125;
&#125;


Unobfustcated result:
method:System.Action`1&#91;System.Guid&#93;
param:System.Guid arg1
RuntimeParameterInfo.DefaultValue:&#40;null&#41;
Success


Obfustcated result:
method:System.Action`1&#91;System.Guid&#93;
param:System.Guid &#9787;
RuntimeParameterInfo.DefaultValue:
Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. &#40;Exception from HRESULT: 0x8007000B&#41;
   at System.Reflection.MetadataImport._GetDefaultValue&#40;IntPtr scope, Int32 mdToken, Int64& value, Int32& length, Int32& corElementType&#41;
   at System.Reflection.MdConstant.GetValue&#40;MetadataImport scope, Int32 token, RuntimeTypeHandle fieldTypeHandle, Boolean raw&#41;
   at System.Reflection.RuntimeParameterInfo.GetDefaultValueInternal&#40;Boolean raw&#41;
   at System.Reflection.RuntimeParameterInfo.GetDefaultValue&#40;Boolean raw&#41;
   at &#9787;.&#9827;.&#9786;&#40;&#41;
ribbles
0

Comments

8 comments

  • Chris.Allen
    Can I take a wild guess that this was targeted for .Net 4.0 or above?

    It seems OK for .net 3.5 which, is very strange, I know but may serve as a workaround?
    Chris.Allen
    0
  • ribbles
    Yes - 4.0
    Unfortunately we are unable to target another version of the framework. Was the sample code adequate?
    ribbles
    0
  • Chris.Allen
    yes- the sample code shows the issue perfectly.

    We seem to have an issue with default parameters generally- I have created a new bug but there were some existing bugs of this nature too.
    Chris.Allen
    0
  • ribbles
    It has been 5 months. Any updates?
    ribbles
    0
  • mbrink
    Any udates on this issue ?

    I'm having the same problem with a .NET 4.5.1 targetted MVC / WebAPI project, so i'm unable to run this project through SmartAssembly.

    The response i get calling a WebAPI method results in:
    &#123;"Message":"An error has occurred.","ExceptionMessage":"An attempt was made to load a program with an incorrect format. &#40;Exception from HRESULT: 0x8007000B&#41;","ExceptionType":"System.BadImageFormatException","StackTrace":"   at System.Reflection.MetadataImport._GetDefaultValue&#40;IntPtr scope, Int32 mdToken, Int64& value, Int32& length, Int32& corElementType&#41;
    at System.Reflection.MdConstant.GetValue&#40;MetadataImport scope, Int32 token, RuntimeTypeHandle fieldTypeHandle, Boolean raw&#41;
    at System.Reflection.RuntimeParameterInfo.GetDefaultValueInternal&#40;Boolean raw&#41;
    at System.Reflection.RuntimeParameterInfo.GetDefaultValue&#40;Boolean raw&#41;
    at System.Web.Http.Internal.ParameterInfoExtensions.TryGetDefaultValue&#40;ParameterInfo parameterInfo, Object& value&#41;
    at System.Web.Http.Controllers.ReflectedHttpParameterDescriptor.get_DefaultValue&#40;&#41;
    at System.Web.Http.ModelBinding.ModelBinderParameterBinding.ExecuteBindingAsync&#40;ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken&#41;
    at System.Web.Http.Controllers.HttpActionBinding.&lt;ExecuteBindingAsyncCore&gt;d__0.MoveNext&#40;&#41;--- End of stack trace from previous location where exception was thrown ---
    

    Thanks in advance.
    mbrink
    0
  • Chris.Allen
    No updates, sorry.
    Chris.Allen
    0
  • emh
    We are getting the same error, using SmartAssembly v6.8.0.121 and .Net 4.5.1

    Do you know how can I report this error to redgate? I have been told that redgate priorities errors due to the amount of reports submitted.

    Anyone knows?
    emh
    0
  • yauhen
    We have the same problems in ASP.NET MVC with the same version of smart assembly.

    As far as I could see problem appears for us only in case of nullable parameters with default value null. So as workaround we just removed default null value.
    yauhen
    0

Add comment

Please sign in to leave a comment.