什么是Marshal.GenerateGuidForType(类型)和Type.GUID之间的区别?区别、类型、GenerateGuidForType、Marshal

2023-09-03 16:51:28 作者:做你意中人

 键入classType所= typeof运算(SomeClass的);
布尔等于= Marshal.GenerateGuidForType(classType所)== classType.GUID;
 

我还没有发现这种失败的条件的情况下。

所以为什么以及何时我应该使用的仅仅让元帅方法,而不是在的GUID 属性?

解决方案

看http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.generateguidfortype.aspx

... GenerateGuidForType提供相同的功能的Type.GUID属性。

因此​​根据文档它们是相同的。然而,Marshal.GenerateGuidForType仅适用RuntimeType物体,而Type.GUID提供了一种用于某些其它类型的实现,以及

例如:


使用系统;
使用系统codeDOM。
使用了System.Runtime.InteropServices;
使用System.Workflow.ComponentModel.Compiler;

命名空间示例
{
    类节目
    {
        静态codeCompileUnit BuildHelloWorldGraph()
        {
            VAR compileUnit =新的codeCompileUnit();
            变种样本=新的codeNamespace(样本);
            compileUnit.Namespaces.Add(样本);

            VAR 1级=新的codeTypeDeclaration(1级);
            samples.Types.Add(1级);

            返回compileUnit;
        }


        静态无效的主要(字串[] args)
        {
            VAR单位= BuildHelloWorldGraph();
            VAR typeProvider =新TypeProvider(空);
            typeProvider.Add codeCompileUnit(单位);
            VAR T = typeProvider.GetType(Samples.Class1);
            Console.WriteLine(t.GUID); //输出的GUID设计时类型的实例。
            Console.WriteLine(Marshal.GenerateGuidForType(吨)); //引发的ArgumentException。
        }
    }
}

Type classType = typeof(SomeClass);
bool equal = Marshal.GenerateGuidForType(classType) == classType.GUID;
常见的美国警察有何区别,了解一下

I haven't found a case that fail this condition.

So why and when should I use the Marshal method instead of simply getting the GUID property?

解决方案

see http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.generateguidfortype.aspx

... GenerateGuidForType provides the same functionality as the Type.GUID property.

So according to documentation they are the same. However, Marshal.GenerateGuidForType works only for RuntimeType objects, while Type.GUID is provided for some other Type implementations as well.

E.g.:


using System;
using System.CodeDom;
using System.Runtime.InteropServices;
using System.Workflow.ComponentModel.Compiler;

namespace Samples
{
    class Program
    {
        static CodeCompileUnit BuildHelloWorldGraph()
        {
            var compileUnit = new CodeCompileUnit();
            var samples = new CodeNamespace("Samples");
            compileUnit.Namespaces.Add(samples);

            var class1 = new CodeTypeDeclaration("Class1");
            samples.Types.Add(class1);

            return compileUnit;
        }


        static void Main(string[] args)
        {
            var unit = BuildHelloWorldGraph();
            var typeProvider = new TypeProvider(null);
            typeProvider.AddCodeCompileUnit(unit);
            var t = typeProvider.GetType("Samples.Class1");
            Console.WriteLine(t.GUID); // prints GUID for design time type instance.
            Console.WriteLine(Marshal.GenerateGuidForType(t)); // throws ArgumentException.
        }
    }
}

 
精彩推荐
图片推荐