-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The ConstructorBuilder and MethodBuilder generated parameter should not has default value by default. #20909
Comments
It can cause some wired behaviors for the Autofac integrating with DynamicProxy2 when the dependency is not registered. Since there is a class just make use of it. |
@joshfree can you please triage and close/move to future/2.1 or assign for ZBB |
dotnet/corefx#17943 dotnet/corefx#12338 dotnet/corefx#11797
I agree that HasDefaultValue==false would be the better default in order to distinguish between an explicit default value and an implied default value other than null. If the default value is changed to For example, changing the same code object to use a non-nullable var methodBuilder = type.DefineMethod("DoSomething", MethodAttributes.Public, CallingConventions.Standard, typeof(int), new[] { typeof(int) });
il = methodBuilder.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ret); along with consuming code that would try to use the default value (null) as the argument var typeInfo = type.CreateTypeInfo();
var constructor = typeInfo.GetConstructor(new[] { typeof(int) });
var parameters = constructor.GetParameters();
var method = typeInfo.GetMethod("DoSomething", new[] { typeof(int) });
parameters = method.GetParameters();
Type? realType = type.CreateType();
object obj = Activator.CreateInstance(realType, new object[] { null });
MethodInfo mi = realType.GetMethod("DoSomething");
object defaultValue = mi.GetParameters()[0].DefaultValue;
// defaultValue is null
int myInt = (int)mi.Invoke(obj, new object[] { defaultValue });
// myInt is 0 even though the default value is "null" |
Moving to future based on priority + schedule. |
Moving to 8.0; seems like it should be a trivial fix. |
The HasDefaultValue of parameters should be false by default, for the constructors and methods generated from System.Reflection.Emit.ConstructorBuilder and System.Reflection.Emit.MethodBuilder. But actually the HasDefaultValue of the parameters are true. It means the emitter generates parameter default values as null unexpected. The sample code as following:
But the DynamicMethod has the correct parameter default value as expected. The sample code as following:
The text was updated successfully, but these errors were encountered: