in src/Microsoft.Diagnostics.Runtime/ClrDelegate.cs [94:142]
private bool Validate()
{
// The overall goal here is to throw an exception when the user created a ClrDelegate class on an object that isn't
// a delegate. However, we DON'T want to throw when we are failing to find the right fields due to missing metadata.
// Note it's ok to be slow in this method because we are down a failure path that should usually not happen in practice.
if (Object.Type is null)
return false;
// If we have no fields then this isn't a delegate.
if (Object.Type.Fields.Length == 0)
return false;
// Assume this is a valid
bool seenAny = false;
bool allNull = true;
foreach (ClrInstanceField field in Object.Type.Fields)
{
seenAny |= field.Name is "_methodPtr" or "_methodPtrAux";
allNull &= field.Name is null;
}
// If all field names were null then we cannot validate whether this was a delegate or not. The case we are worried
// about here is if we have no
if (allNull)
return true;
// If we saw fields we expected return it's valid even if we didn't understand this delegate.
if (seenAny)
return true;
ClrType? curr = Object.Type;
for (int i = 0; i < 8 && curr != null; i++, curr = curr.BaseType)
{
// If we found System.Delegate, we are done.
if (curr.Name == DelegateType)
return true;
// If we found a blank name in mscorlib then we have a metadata problem, and we cannot validate this delegate.
// Don't throw an exception in this case.
if (curr.Name == null && curr.Module == Object.Type.Heap.Runtime.BaseClassLibrary)
return true;
}
// We are definitely not a delegate.
return false;
}