-
Notifications
You must be signed in to change notification settings - Fork 87
/
ExportedDelegate.cs
42 lines (35 loc) · 1.13 KB
/
ExportedDelegate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.VisualStudio.Composition
{
using System;
using System.Linq.Expressions;
using System.Reflection;
public class ExportedDelegate
{
private readonly object target;
private readonly MethodInfo method;
public ExportedDelegate(object target, MethodInfo method)
{
Requires.NotNull(method, nameof(method));
this.target = target;
this.method = method;
}
public Delegate CreateDelegate(Type delegateType)
{
Requires.NotNull(delegateType, nameof(delegateType));
if (delegateType == typeof(Delegate) || delegateType == typeof(MulticastDelegate))
{
delegateType = ReflectionHelpers.GetContractTypeForDelegate(this.method);
}
try
{
return this.method.CreateDelegate(delegateType, this.target);
}
catch (ArgumentException)
{
// Bind failure occurs return null;
return null;
}
}
}
}