什么是Marshal.GenerateGuidForType(类型)和Type.GUID之间的区别? [英] What's the difference between Marshal.GenerateGuidForType(Type) and Type.GUID?

查看:201
本文介绍了什么是Marshal.GenerateGuidForType(类型)和Type.GUID之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

解决方案

看<一href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.generateguidfortype.aspx">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.
        }
    }
}

这篇关于什么是Marshal.GenerateGuidForType(类型)和Type.GUID之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆