public static bool TryCast(Type contractType, object value, out object result) { if (value == null) { result = null; return(true); } if (contractType.IsInstanceOfType(value)) { result = value; return(true); } // We couldn't cast see if a delegate works for us. if (typeof(Delegate).IsAssignableFrom(contractType)) { ExportedDelegate exportedDelegate = value as ExportedDelegate; if (exportedDelegate != null) { result = exportedDelegate.CreateDelegate(contractType); return(result != null); } } result = null; return(false); }
/// <summary> /// Initializes a new instance of the <see cref="DiagnosticSensor" /> class. /// </summary> internal DiagnosticSensor(ExportedDelegate export) { if (export == null) { throw new ArgumentNullException("export"); } @delegate = export.CreateDelegate(typeof(Delegate)); name = GetName(@delegate.Method); declaringType = @delegate.Method.DeclaringType; returnType = @delegate.Method.ReturnType; ValidateAndInitialize(); }