Skip to content

Instantly share code, notes, and snippets.

@Keboo
Created May 30, 2019 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Keboo/414d2d84ae3c3572844e19f2ebec61d8 to your computer and use it in GitHub Desktop.
Save Keboo/414d2d84ae3c3572844e19f2ebec61d8 to your computer and use it in GitHub Desktop.
Create a delegate instance from its System.Type
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading.Tasks;
namespace CreateDelegate
{
[TestClass]
public class UnitTest1
{
private struct MyStruct
{
public string Value { get; set; }
}
private delegate bool HasOutParameter(out int foo);
private delegate bool HasRefParameter(ref int foo);
[TestMethod]
public void WorksWithAction()
{
Action @delegate = (Action) CreateEmptyDelegate(typeof(Action));
@delegate();
}
[TestMethod]
public void WorksWithActionParameters()
{
Action<int> @delegate = (Action<int>)CreateEmptyDelegate(typeof(Action<int>));
@delegate(42);
}
[TestMethod]
public void WorksWithIntReturn()
{
Func<int> @delegate = (Func<int>)CreateEmptyDelegate(typeof(Func<int>));
Assert.AreEqual(0, @delegate());
}
[TestMethod]
public void WorksWithStructReturn()
{
Func<MyStruct> @delegate = (Func<MyStruct>)CreateEmptyDelegate(typeof(Func<MyStruct>));
Assert.AreEqual(default, @delegate());
}
[TestMethod]
public void WorksWithClassReturn()
{
Func<object> @delegate = (Func<object>)CreateEmptyDelegate(typeof(Func<object>));
Assert.AreEqual(null, @delegate());
}
[TestMethod]
public async Task WorksWithAsyncMethod()
{
Func<Task> @delegate = (Func<Task>)CreateEmptyDelegate(typeof(Func<Task>));
await @delegate();
}
[TestMethod]
public async Task WorksWithAsyncMethodWithReturn()
{
Func<Task<int>> @delegate = (Func<Task<int>>)CreateEmptyDelegate(typeof(Func<Task<int>>));
Assert.AreEqual(0, await @delegate());
}
[TestMethod]
public async Task WorksWithAsyncMethodWithStructReturn()
{
Func<Task<MyStruct>> @delegate = (Func<Task<MyStruct>>)CreateEmptyDelegate(typeof(Func<Task<MyStruct>>));
Assert.AreEqual(default, await @delegate());
}
[TestMethod]
public async Task WorksWithAsyncMethodWithClassReturn()
{
Func<Task<object>> @delegate = (Func<Task<object>>)CreateEmptyDelegate(typeof(Func<Task<object>>));
Assert.AreEqual(null, await @delegate());
}
[TestMethod]
public void WorksWithOutParameter()
{
HasOutParameter @delegate = (HasOutParameter)CreateEmptyDelegate(typeof(HasOutParameter));
Assert.IsFalse(@delegate(out int intValue));
Assert.AreEqual(0, intValue);
}
[TestMethod]
public void WorksWithRefParameter()
{
int foo = 42;
HasRefParameter @delegate = (HasRefParameter)CreateEmptyDelegate(typeof(HasRefParameter));
Assert.IsFalse(@delegate(ref foo));
Assert.AreEqual(42, foo);
}
public static Delegate CreateEmptyDelegate(Type delegateType)
{
if (delegateType == null) throw new ArgumentNullException(nameof(delegateType));
if (!delegateType.IsSubclassOf(typeof(Delegate)))
{
throw new ArgumentException($"{delegateType.FullName} is not a delegate type");
}
//TODO: Implement
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment