调用构造函数时使用字符串作为类型参数
Using string as parameter for type when calling constructor
我正在尝试创建一个基于字符串参数的类型并将其传递到构造函数的类型参数中。当仅使用 if 语句检查它时,它变得非常讨厌,我不知道如何以编程方式/通用方式进行更多操作。
我尝试过反射,但只有 returns 一个对象并将对象传递给 < T > 显然是行不通的。
有没有人知道如何在没有数千个 if 语句的情况下以更精细的方式解决这个问题?
对象创建如下所示:
if (Options.Input1Type == "int" && Options.Output1Type == "int") return BlockBuilder.Build<int, int>(Kind, Options, TransformToSelf);
if (Options.Input1Type == "bool" && Options.Output1Type == "bool") return BlockBuilder.Build<bool, bool>(Kind, Options, TransformToSelf);
if (Options.Input1Type == "string" && Options.Output1Type == "string") return BlockBuilder.Build<string, string>(Kind, Options, TransformToSelf);
if (Options.Input1Type == "bool" && Options.Output1Type == "int") return BlockBuilder.Build<bool, int>(Kind, Options, TransformToInt);
if (Options.Input1Type == "bool" && Options.Output1Type == "string") return BlockBuilder.Build<bool, string>(Kind, Options, TransformToString);
if (Options.Input1Type == "int" && Options.Output1Type == "bool") return BlockBuilder.Build<int, bool>(Kind, Options, TransformToBool);
if (Options.Input1Type == "int" && Options.Output1Type == "string") return BlockBuilder.Build<int, string>(Kind, Options, TransformToString);
if (Options.Input1Type == "string" && Options.Output1Type == "int") return BlockBuilder.Build<string, int>(Kind, Options, TransformToInt);
if (Options.Input1Type == "string" && Options.Output1Type == "bool") return BlockBuilder.Build<string, bool>(Kind, Options, TransformToBool);
BlockBuilder 看起来像这样:
public static IDataflowBlock Build<TIn, TOut>(string kind, BlockOptions blockOptions, Func<TIn, TOut> singleOutputExecutionFunction = null, Func<TIn, IEnumerable<TOut>> multipleOutputExecutionFunction = null)
{
if (singleOutputExecutionFunction == null && multipleOutputExecutionFunction == null)
throw new ArgumentException("Missing function to execute");
Enum.TryParse(kind, out TransformationBlocks Kind);
switch (Kind)
{
case TransformationBlocks.Undefined:
throw new ArgumentException("No block type was specified");
case TransformationBlocks.TransformBlock:
return new TransformBlock<TIn, TOut>(param => { return singleOutputExecutionFunction(param); }, new ExecutionDataflowBlockOptions()
{
MaxMessagesPerTask = blockOptions.MaxMessagesPerTask,
BoundedCapacity = blockOptions.BoundedCapacity,
MaxDegreeOfParallelism = blockOptions.MaxDegreeOfParallelism,
});
case TransformationBlocks.TransformManyBlock:
return new TransformManyBlock<TIn, TOut>(param => { return multipleOutputExecutionFunction(param); }, new ExecutionDataflowBlockOptions()
{
MaxMessagesPerTask = blockOptions.MaxMessagesPerTask,
BoundedCapacity = blockOptions.BoundedCapacity,
MaxDegreeOfParallelism = blockOptions.MaxDegreeOfParallelism,
});
default:
return default;
}
}
委托/函数如下所示:
private static T TransformToSelf<T>(T obj)
{
return obj;
}
private static string TransformToString<T>(T obj)
{
return Convert.ToString(obj);
}
private static int TransformToInt<T>(T obj)
{
return Convert.ToInt32(obj);
}
private static bool TransformToBool<T>(T obj)
{
return Convert.ToBoolean(obj);
}
这并不容易,但它是可行的。
如果可以将 Input1Type
和 Input2Type 的类型更改为 System.Type 而不是字符串,那就容易多了。
如果没有,那么我建议您按照@neil 的建议创建一个映射函数,将字符串映射到类型,然后使用MethodInfo.MakeGenericType()
调用您的Build()
函数。
下面是 MakeGenericType()
的简单示例。
using System;
using System.Reflection;
namespace make_generic_type
{
class Program
{
static void Main(string[] args)
{
// Normal C# usage
var host = new Host();
Console.WriteLine(host.GenericMethod<int, string>("Test"));
// Use reflection to get type definition
var unboundMethod = typeof(Host).GetMethod(nameof(Host.GenericMethod));
// As the method is generic, you need to pass the type parameters in.
// We do this by binding the type parameters with MethodInfo.MakeGenericMethod();
var boundMethod = unboundMethod.MakeGenericMethod(new Type[]{ typeof(int), typeof(string) });
// Now we have a method that we can invoke via reflection as normal
Console.WriteLine(boundMethod.Invoke(new Host(), new object[]{ "Test"}));
}
}
class Host{
public string GenericMethod<TIn, TOut>(string kind)
{
return $"{typeof(TIn).Name}; {typeof(TOut).Name}; {kind};";
}
}
}
我正在尝试创建一个基于字符串参数的类型并将其传递到构造函数的类型参数中。当仅使用 if 语句检查它时,它变得非常讨厌,我不知道如何以编程方式/通用方式进行更多操作。
我尝试过反射,但只有 returns 一个对象并将对象传递给 < T > 显然是行不通的。
有没有人知道如何在没有数千个 if 语句的情况下以更精细的方式解决这个问题?
对象创建如下所示:
if (Options.Input1Type == "int" && Options.Output1Type == "int") return BlockBuilder.Build<int, int>(Kind, Options, TransformToSelf);
if (Options.Input1Type == "bool" && Options.Output1Type == "bool") return BlockBuilder.Build<bool, bool>(Kind, Options, TransformToSelf);
if (Options.Input1Type == "string" && Options.Output1Type == "string") return BlockBuilder.Build<string, string>(Kind, Options, TransformToSelf);
if (Options.Input1Type == "bool" && Options.Output1Type == "int") return BlockBuilder.Build<bool, int>(Kind, Options, TransformToInt);
if (Options.Input1Type == "bool" && Options.Output1Type == "string") return BlockBuilder.Build<bool, string>(Kind, Options, TransformToString);
if (Options.Input1Type == "int" && Options.Output1Type == "bool") return BlockBuilder.Build<int, bool>(Kind, Options, TransformToBool);
if (Options.Input1Type == "int" && Options.Output1Type == "string") return BlockBuilder.Build<int, string>(Kind, Options, TransformToString);
if (Options.Input1Type == "string" && Options.Output1Type == "int") return BlockBuilder.Build<string, int>(Kind, Options, TransformToInt);
if (Options.Input1Type == "string" && Options.Output1Type == "bool") return BlockBuilder.Build<string, bool>(Kind, Options, TransformToBool);
BlockBuilder 看起来像这样:
public static IDataflowBlock Build<TIn, TOut>(string kind, BlockOptions blockOptions, Func<TIn, TOut> singleOutputExecutionFunction = null, Func<TIn, IEnumerable<TOut>> multipleOutputExecutionFunction = null)
{
if (singleOutputExecutionFunction == null && multipleOutputExecutionFunction == null)
throw new ArgumentException("Missing function to execute");
Enum.TryParse(kind, out TransformationBlocks Kind);
switch (Kind)
{
case TransformationBlocks.Undefined:
throw new ArgumentException("No block type was specified");
case TransformationBlocks.TransformBlock:
return new TransformBlock<TIn, TOut>(param => { return singleOutputExecutionFunction(param); }, new ExecutionDataflowBlockOptions()
{
MaxMessagesPerTask = blockOptions.MaxMessagesPerTask,
BoundedCapacity = blockOptions.BoundedCapacity,
MaxDegreeOfParallelism = blockOptions.MaxDegreeOfParallelism,
});
case TransformationBlocks.TransformManyBlock:
return new TransformManyBlock<TIn, TOut>(param => { return multipleOutputExecutionFunction(param); }, new ExecutionDataflowBlockOptions()
{
MaxMessagesPerTask = blockOptions.MaxMessagesPerTask,
BoundedCapacity = blockOptions.BoundedCapacity,
MaxDegreeOfParallelism = blockOptions.MaxDegreeOfParallelism,
});
default:
return default;
}
}
委托/函数如下所示:
private static T TransformToSelf<T>(T obj)
{
return obj;
}
private static string TransformToString<T>(T obj)
{
return Convert.ToString(obj);
}
private static int TransformToInt<T>(T obj)
{
return Convert.ToInt32(obj);
}
private static bool TransformToBool<T>(T obj)
{
return Convert.ToBoolean(obj);
}
这并不容易,但它是可行的。
如果可以将 Input1Type
和 Input2Type 的类型更改为 System.Type 而不是字符串,那就容易多了。
如果没有,那么我建议您按照@neil 的建议创建一个映射函数,将字符串映射到类型,然后使用MethodInfo.MakeGenericType()
调用您的Build()
函数。
下面是 MakeGenericType()
的简单示例。
using System;
using System.Reflection;
namespace make_generic_type
{
class Program
{
static void Main(string[] args)
{
// Normal C# usage
var host = new Host();
Console.WriteLine(host.GenericMethod<int, string>("Test"));
// Use reflection to get type definition
var unboundMethod = typeof(Host).GetMethod(nameof(Host.GenericMethod));
// As the method is generic, you need to pass the type parameters in.
// We do this by binding the type parameters with MethodInfo.MakeGenericMethod();
var boundMethod = unboundMethod.MakeGenericMethod(new Type[]{ typeof(int), typeof(string) });
// Now we have a method that we can invoke via reflection as normal
Console.WriteLine(boundMethod.Invoke(new Host(), new object[]{ "Test"}));
}
}
class Host{
public string GenericMethod<TIn, TOut>(string kind)
{
return $"{typeof(TIn).Name}; {typeof(TOut).Name}; {kind};";
}
}
}