-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[XC] better support for nullable props and BPs #28550
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1410,6 +1410,25 @@ static IEnumerable<Instruction> SetBinding(VariableDefinition parent, FieldRefer | |
|
||
static bool CanSetValue(FieldReference bpRef, bool attached, INode node, IXmlLineInfo iXmlLineInfo, ILContext context) | ||
{ | ||
static bool CanSetValue (TypeReference bpTypeRef, VariableDefinition varValue, ILContext context, IXmlLineInfo iXmlLineInfo) | ||
{ | ||
// If it's an attached BP, there's no second chance to handle IMarkupExtensions, so we try here. | ||
// Worst case scenario ? InvalidCastException at runtime | ||
if (varValue.VariableType.FullName == "System.Object") | ||
return true; | ||
var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(context.Cache, bpTypeRef, context.Body.Method.Module); | ||
if (implicitOperator != null) | ||
return true; | ||
|
||
//as we're in the SetValue Scenario, we can accept value types, they'll be boxed | ||
if (varValue.VariableType.IsValueType && bpTypeRef.FullName == "System.Object") | ||
return true; | ||
|
||
if (varValue.VariableType.InheritsFromOrImplements(context.Cache, bpTypeRef) || varValue.VariableType.FullName == "System.Object") | ||
return true; | ||
return false; | ||
} | ||
|
||
var module = context.Body.Method.Module; | ||
|
||
if (bpRef == null) | ||
|
@@ -1424,22 +1443,23 @@ static bool CanSetValue(FieldReference bpRef, bool attached, INode node, IXmlLin | |
if (!context.Variables.TryGetValue(elementNode, out VariableDefinition varValue)) | ||
return false; | ||
|
||
|
||
var bpTypeRef = bpRef.GetBindablePropertyType(context.Cache, iXmlLineInfo, module); | ||
// If it's an attached BP, there's no second chance to handle IMarkupExtensions, so we try here. | ||
// Worst case scenario ? InvalidCastException at runtime | ||
if (varValue.VariableType.FullName == "System.Object") | ||
return true; | ||
var implicitOperator = varValue.VariableType.GetImplicitOperatorTo(context.Cache, bpTypeRef, module); | ||
if (implicitOperator != null) | ||
return true; | ||
|
||
//as we're in the SetValue Scenario, we can accept value types, they'll be boxed | ||
if (varValue.VariableType.IsValueType && bpTypeRef.FullName == "System.Object") | ||
if (CanSetValue(bpTypeRef, varValue, context, iXmlLineInfo)) | ||
return true; | ||
|
||
return varValue.VariableType.InheritsFromOrImplements(context.Cache, bpTypeRef) || varValue.VariableType.FullName == "System.Object"; | ||
if (bpTypeRef.ResolveCached(context.Cache).FullName == "System.Nullable`1") | ||
{ | ||
bpTypeRef = ((GenericInstanceType)bpTypeRef).GenericArguments[0]; | ||
if (CanSetValue(bpTypeRef, varValue, context, iXmlLineInfo)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
|
||
static bool CanGetValue(VariableDefinition parent, FieldReference bpRef, bool attached, IXmlLineInfo iXmlLineInfo, ILContext context, out TypeReference propertyType) | ||
{ | ||
var module = context.Body.Method.Module; | ||
|
@@ -1483,32 +1503,37 @@ static IEnumerable<Instruction> SetValue(VariableDefinition parent, FieldReferen | |
var @else = Create(OpCodes.Nop); | ||
var endif = Create(OpCodes.Nop); | ||
|
||
if (context.Variables[elementNode].VariableType.FullName == "System.Object") | ||
|
||
var varValue = context.Variables[elementNode]; | ||
if (varValue.VariableType.FullName == "System.Object") | ||
{ | ||
//if(value != null && value.GetType().IsAssignableFrom(typeof(BindingBase))) | ||
yield return Create(Ldloc, context.Variables[elementNode]); | ||
yield return Create(Ldloc, varValue); | ||
yield return Create(Brfalse, @else); | ||
|
||
yield return Create(Ldtoken, module.ImportReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "BindingBase"))); | ||
yield return Create(Call, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Type"), methodName: "GetTypeFromHandle", parameterTypes: new[] { ("mscorlib", "System", "RuntimeTypeHandle") }, isStatic: true)); | ||
yield return Create(Ldloc, context.Variables[elementNode]); | ||
yield return Create(Ldloc, varValue); | ||
yield return Create(Callvirt, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Object"), methodName: "GetType", paramCount: 0)); | ||
yield return Create(Callvirt, module.ImportMethodReference(context.Cache, ("mscorlib", "System", "Type"), methodName: "IsAssignableFrom", parameterTypes: new[] { ("mscorlib", "System", "Type") })); | ||
yield return Create(Brfalse, @else); | ||
//then | ||
yield return Create(Ldloc, context.Variables[elementNode]); | ||
yield return Create(Ldloc, varValue); | ||
yield return Create(Br, endif); | ||
//else | ||
yield return @else; | ||
} | ||
var bpTypeRef = bpRef.GetBindablePropertyType(context.Cache, iXmlLineInfo, module); | ||
foreach (var instruction in context.Variables[elementNode].LoadAs(context.Cache, bpTypeRef, module)) | ||
foreach (var instruction in varValue.LoadAs(context.Cache, bpTypeRef, module)) | ||
yield return instruction; | ||
if (bpTypeRef.IsValueType) | ||
{ | ||
if ( bpTypeRef.ResolveCached(context.Cache).FullName == "System.Nullable`1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the main change |
||
&& TypeRefComparer.Default.Equals(varValue.VariableType, ((GenericInstanceType)bpTypeRef).GenericArguments[0])) | ||
bpTypeRef = ((GenericInstanceType)bpTypeRef).GenericArguments[0]; | ||
yield return Create(Box, module.ImportReference(bpTypeRef)); | ||
|
||
} | ||
//endif | ||
if (context.Variables[elementNode].VariableType.FullName == "System.Object") | ||
if (varValue.VariableType.FullName == "System.Object") | ||
yield return endif; | ||
|
||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extracting the logic in a local method to run it twice, once on the nullable type, once on the inner type