简体   繁体   English

以下C#代码有什么作用?

[英]What does the following C# code do?

I ran across the following class in a C# XNA graphics api and I am not sure what it does or that it needs to be so obscure. 我在C#XNA图形api中遇到了以下类,我不确定它是做什么的,或者它需要如此模糊。 (T is constrained to be a struct in a parent class) (T被约束为父类中的结构)

    static class Ident
    {
        static object sync = new object();
        static volatile int index = 0;
        static int Index
        {
            get
            {
                lock (sync)
                    return index++;
            }
        }
        class Type<T>
        {
            public static int id = Index;
        }
        public static int TypeIndex<T>()
        {
            return Type<T>.id;
        }
    }

The API makes only on call to this static class: int index = Ident.TypeIndex<T>(); API仅在调用此静态类时执行: int index = Ident.TypeIndex<T>();

It assigns an application-wide (static) and thread-safe(locking sync object + volatile index) unique identifier for each different type T. 它为每个不同类型T分配应用程序范围(静态)和线程安全(锁定同步对象+易失性索引)唯一标识符。

example : 例如:

Console.WriteLine(Ident.TypeIndex<int>()); // 0
Console.WriteLine(Ident.TypeIndex<string>()); // 1
Console.WriteLine(Ident.TypeIndex<long>()); // 2
Console.WriteLine(Ident.TypeIndex<int>()); // 0

Volatile is being used to ensure that the current thread doesn't cache the index value and locking prevents more than one thread to access it. Volatile用于确保当前线程不会缓存索引值,并且锁定会阻止多个线程访问它。

This creates a unique integer identifier for each Type, based on the order that it's accessed, in a thread-safe manner. 这将以线程安全的方式基于每个类型访问的顺序为每个类型创建唯一的整数标识符。

For example, if you do: 例如,如果你这样做:

int myClassId = Ident.TypeIndex<MyClass>();
int mySecondClssId = Ident.TypeIndex<MySecondClass>();

You'll get 2 "TypeIndex" numbers (with mySecondClassId being at least 1 more than myClassId, but potentially greater, due to threading). 您将得到2个“TypeIndex”数字(mySecondClassId至少比myClassId多1个,但由于线程可能更大)。 Later, if you call this again with the same class, it will return the same TypeIndex for that class. 稍后,如果再次使用同一个类调用它,它将返回该类的相同TypeIndex。

For example, if I run this, using: 例如,如果我运行它,使用:

Console.WriteLine(Ident.TypeIndex<Program>());
Console.WriteLine(Ident.TypeIndex<Test>());
Console.WriteLine(Ident.TypeIndex<Program>());
Console.WriteLine(Ident.TypeIndex<Test>());

It will print: 它将打印:

0
1
0
1

However, this could be done more effectively using Interlocked.Increment , which would avoid the need for the lock and the synchronization object completely. 但是,使用Interlocked.Increment可以更有效地完成此操作,这将完全避免锁和同步对象的需要。 The following gives exactly the same answer, with no locking required: 以下给出了完全相同的答案,不需要锁定:

static class Ident
{
    static int index = -1;
    static int Index
    {
        get
        {

            return Interlocked.Increment(ref index);
        }
    }
    private static class Type<T>
    {
        public static int id = Index;
    }

    public static int TypeIndex<T>()
    {
        return Type<T>.id;
    }
} 

It returns the number of times Ident.TypeIndex was called, probably to assign a unique number to each object. 它返回调用Ident.TypeIndex的次数,可能是为每个对象分配一个唯一的编号。

Because of the way they are using generics, there should be a different sequence of numbers for every type T. So you could have a #1 circle, a #2 circle, and a #1 square. 由于他们使用泛型的方式,每种类型T应该有一个不同的数字序列。所以你可以有一个#1圆,一个#2圆和一个#1正方形。

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

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