C# 我想将object的实例转换为仅在运行时已知的类型的实例

C# 我想将object的实例转换为仅在运行时已知的类型的实例,c#,C#,工作原理如下: object myObjet = GetObject(); Type myType = GetType(); var x = ConvertToType(myObject, myType); object ConvertToType(object source, Type targetType) { var sourceType = source.GetType(); if (sourceType == typeof(Something) &&

工作原理如下:

object myObjet = GetObject();
Type myType = GetType();

var x = ConvertToType(myObject, myType);
object ConvertToType(object source, Type targetType)
{
    var sourceType = source.GetType();

    if (sourceType == typeof(Something) && targetType == typeof(SomethingElse))
    {
        // call some function that takes the input and converts it to the output type.
        return ConvertSomethingToSomethingElse((Something) source);
    }

    // Repeat the above, over and over. Every time there's a new type conversion, 
    // add it.

    throw new Exception($"Can't convert {sourceType} to {targetType}");
}
object ConvertToType<T>(object source, Type targetType)
{
    return Mapper.Map(source, source.GetType(), targetType);
}

myobject可以是原始对象或其他对象。如果转换不可能,则会引发异常。框架中是否有内置的内容。System.Convert类要求您知道目标类型。

您可以准确地编写所描述的方法。可能是这样的:

object myObjet = GetObject();
Type myType = GetType();

var x = ConvertToType(myObject, myType);
object ConvertToType(object source, Type targetType)
{
    var sourceType = source.GetType();

    if (sourceType == typeof(Something) && targetType == typeof(SomethingElse))
    {
        // call some function that takes the input and converts it to the output type.
        return ConvertSomethingToSomethingElse((Something) source);
    }

    // Repeat the above, over and over. Every time there's a new type conversion, 
    // add it.

    throw new Exception($"Can't convert {sourceType} to {targetType}");
}
object ConvertToType<T>(object source, Type targetType)
{
    return Mapper.Map(source, source.GetType(), targetType);
}
如果有很多可能的转换,这可能会变得丑陋。(这种方法有意义的转换次数是否完美?我倾向于否定,但这是一种观点。)

你可以用。您的方法如下所示:

object myObjet = GetObject();
Type myType = GetType();

var x = ConvertToType(myObject, myType);
object ConvertToType(object source, Type targetType)
{
    var sourceType = source.GetType();

    if (sourceType == typeof(Something) && targetType == typeof(SomethingElse))
    {
        // call some function that takes the input and converts it to the output type.
        return ConvertSomethingToSomethingElse((Something) source);
    }

    // Repeat the above, over and over. Every time there's a new type conversion, 
    // add it.

    throw new Exception($"Can't convert {sourceType} to {targetType}");
}
object ConvertToType<T>(object source, Type targetType)
{
    return Mapper.Map(source, source.GetType(), targetType);
}
对象转换类型(对象源,类型targetType)
{
返回Mapper.Map(source,source.GetType(),targetType);
}
您仍然需要配置所有映射,在某些情况下还需要编写函数来执行希望支持的所有可能的转换组合。这是绕不开的

这两种方法都说明,虽然答案是“是的,你可以做到”,但没有免费的午餐。无论您如何分割它,您仍然必须编写代码来处理所有转换。如果您已经在做所有这些,那么让所有这些通过一个方法进行是否有意义?可能有一些场景(或者,
Map
重载甚至不存在),但在大多数情况下,我怀疑这是否有用


这不是我通常推荐的模式,因为它意味着类型不安全的代码将调用此方法,结果是一个类型也不安全的
对象。如果可能的话,因为您仍然需要编写类型安全代码来进行转换,所以我将直接使用这些方法,而不是使用
object

这样行吗?您想将
x
静态键入吗?如果存在这样的方法,您将如何以及为什么使用它?我的第一反应是解释为什么我们不应该这样做,但这意味着我在做假设。我怀疑编写和使用此方法会在调用该方法和尝试使用返回的结果时导致复杂的代码,因为这意味着所有内容都声明为
对象
,并且代码不是类型安全的。方法本身是最简单的部分。这是一堆“如果myobject是这个类型,mytype是那个类型,那么进行这个转换并返回它。”如果这些都不满足,最后一行抛出一个异常。