تعطيل التحقق من الامتثال CLS في C#
-
28-09-2019 - |
سؤال
أنا أعمل على الرمز الذي يحتوي على السمات التالية في بعض أساليبها:
[CLSCompliantAttribute(false)]
كيف يتم ذلك عندما أقوم بإنشاء الكود كما هو ، أرى أن التحقق من الامتثال يتم تنفيذه ، وعندما أعلقه ، يبدو أن التحقق من الامتثال لا يتم تنفيذه؟
لقد توقعت السلوك المعاكس ...
المحلول
مضيفا [CLSCompliant(false)]
يمثل العضو الذي تضيفه إلى أنه غير متوافق.
إذا قمت بتوصيل العضو على أنه غير متوافق ، فلن يحذرك المترجم إذا لم يكن متوافقًا. (بما أنك قلت بالفعل إنه غير متوافق.)
ومع ذلك ، إذا تم وضع علامة على العضو على أنه متوافق (إما بشكل صريح أو غير مباشر من سمة على مستوى التجميع) ، لكنه في الواقع غير متوافق (على سبيل المثال ، يأخذ أ uint
) ، سوف يحذرك المترجم (لأن السمة تكذب الآن حول العضو).
نصائح أخرى
يمكنك إضافته إلى AssemblyInfo.cs على سبيل المثال ، وتجميع جميع التجميع:*. يحب :
using System;
using System.Reflection;
using System.Runtime.InteropServices;
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCulture("")]
[assembly: CLSCompliant(false)]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d29c53b6-88e4-4b33-bb86-f39b4c733542")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]