开发者

C# class definition unknown

I am looking through some C# code and I am seeing something I cannot figure out near the class definition. Here is a sample of what I am seeing.

[MethodImpl(MethodImplOptions.Synchron开发者_JAVA技巧ized)]
public void AddTag(RTag tag)
{
    this.tags.Add(tag)
}

What the heck is the first line doing or stating? I have not been able to track it down in any of my reference books.

Thanks!


The first line is an attribute, i.e. meta data attached to the method.

The MethodImplAttribute specifies the details of how a method is implemented. In particular, MethodImplOptions.Synchronized

Specifies that the method can be executed by only one thread at a time. Static methods lock on the type, whereas instance methods lock on the instance. Only one thread can execute in any of the instance functions, and only one thread can execute in any of a class's static functions.


Before I point you to the documentation for the class, a tip when seeing attributes like this, and this is an attribute:

[SomeName]

or

[SomeName(...)]

then typically, the actual class name is SomeNameAttribute. When using attributes, if the class name ends with the word Attribute, you can leave that ending out.

The class in question for your example is most likely MethodImplAttribute, though I see you might have spelled it wrong, missing the ending L letter.


It is marking the method in such a way that it can only be called from one method at a time:

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions(VS.71).aspx

This is equivelant to performing a lock at the start of the method and releasing the lock at the end of the method.


[MethodImp(methodImpOptions.Synchronized)] Is an attribute applied to the method... probably it is defined in a referenced library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜