简体   繁体   English

泛型函数和带有Type参数的函数之间的区别

[英]Difference between generic function and function with Type parameter

I just wondered what is the difference between using a function that takes a parameter of type Type (and some more parameter) and a generic function that just wants the type in angle bracket ? 我只是想知道使用带有Type参数(和更多参数)的函数与只需要angle bracket的类型的泛型函数之间有什么区别?

Idea behinde: I want to write a function that saves a type on ma filesystem as XML file. 背后的想法:我想编写一个函数来将ma文件系统上的类型另存为XML文件。 Since the XmlSerializer needs to have the type of object to serialize, I want to know: What is better? 由于XmlSerializer需要具有要序列化的对象的类型,因此我想知道:哪个更好?

private bool SerializeIt(object o, Type t, string filePath)
{
    bool result = false;
    try
    {
        if (File.Exists(filePath))
            File.Delete(filePath);

        XmlSerializer serializer = new XmlSerializer(t);
        using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
        {
            serializer.Serialize(fs, o);
        }
        result = true;
    }
    catch (Exception ex)
    {
        result = false;
        Debug.WriteLine(ex);
    }

    return result;
}

private bool SerializeIt<T>(T o, string filePath)
{
    bool result = false;
    try
    {
        if (File.Exists(filePath))
            File.Delete(filePath);

        XmlSerializer serializer = new XmlSerializer(o.GetType());
        using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write))
        {
            serializer.Serialize(fs, o);
        }
    }
    catch (Exception ex)
    {
        result = false;
        Debug.WriteLine(ex);
    }

    return result;
}

Your first method will only work with the type SomeData1 , since that is the type you specify. 您的第一种方法仅适用于SomeData1类型,因为这是您指定的类型。 The other method works with any type passed through T . 另一种方法适用于通过T传递的任何类型。

You could solve the above issue with o.GetType() instead of typeof(SomeData1) . 您可以使用o.GetType()而不是typeof(SomeData1)解决上述问题。 Still, the second method gives you the opportunity to use a base class as serialization base, which will clear out all properties in the deriving class. 尽管如此,第二种方法仍使您有机会使用基类作为序列化基类,这将清除派生类中的所有属性。

Function private bool SerializeIt(object o, Type t, string filePath) is evaluated at runtime. 函数private bool SerializeIt(object o, Type t, string filePath)在运行时评估。 Where function private bool SerializeIt<T>(T o, string filePath is evaluated at compile time. That is inserting the specified type in IL code. 其中,函数private bool SerializeIt<T>(T o, string filePath在编译时评估private bool SerializeIt<T>(T o, string filePath 。即在IL代码中插入指定的类型。

There is a lot of differencies. 有很多差异。 Firstly you can create constraints for generic parameters, eg: 首先,您可以为通用参数创建约束,例如:

private bool SerializeIt<T>(T o, string filePath) where T: ISomething

Secondly - free type inference for generic types. 其次-泛型类型的自由类型推断。

private bool SerializeIt<T>(T o, string filePath)

Something something = new Something();
o.SerializeIt(something, ""); // dont need to pass type (can infer from first argument).

Thirdly - you work on concrete strong type, eg: 第三-您正在研究具体的强类型,例如:

private bool SerializeIt<T>(T o, string filePath)

Generally it makes interface simpler and cleaner. 通常,它使界面更简单,更清洁。 You dont have to use loosely typed objects. 您不必使用松散类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM