قم بتنفيذ واجهة الأعضاء بشكل صريح

An واجهة هو a اتفاق for supporting some functionality. فئات that implement an واجهة must provide the implementation تفاصيل for the الأعضاء specified في the واجهة. For مثال, the IEnumerator واجهة defines the عضو signatures واحد must implement إلى دعم enumerating over a التعيين of الكائنات, such كـ a مجموعة. إلى Implement IEnumerator, a فئة must implement the Current, MoveNext, و Reset الأعضاء.

When an واجهة عضو هو بوضوح implemented بواسطة a فئة, the عضو can be accessed فقط بواسطة using a مرجع إلى the واجهة. This has the effect of hiding the واجهة عضو. A عام reason for بوضوح implementing an واجهة عضو ليس فقط إلى comply مع the واجهة's اتفاق, but also إلى improve it في some way (for مثال, إلى provide strongly-typed وظائف that should be used في place of the واجهة's weakly-typed وظائف). Another عام reason for implementing an واجهة عضو بوضوح هو when the explicit واجهة عضو should not be called بواسطة developers. For مثال, the GetObjectData عضو هو most often بوضوح implemented because it هو called بواسطة the إنشاء تسلسل infrastructure و ليس intended إلى be called من تعليمات برمجية.

The following تصميم guidelines تعليمات ensure that your مكتبة تصميم uses explicit واجهة implementation فقط when appropriate.

Avoid implementing واجهة الأعضاء بوضوح without having a قوي reason إلى do so.

Understanding explicit implementation يتطلب an متقدم المستوى of expertise. For مثال, many developers do not know that an بوضوح implemented عضو هو publicly callable even though its توقيع هو private. Because of this, بوضوح implemented الأعضاء do not appear في the قائمة of publicly مرئي الأعضاء. بوضوح implementing a عضو can also cause unnecessary تحويل إلى كائن of القيمة أنواع.

Consider implementing واجهة الأعضاء بوضوح if the الأعضاء are intended إلى be called فقط through the واجهة.

This mainly يتضمن الأعضاء that دعم the .NET Framework infrastructure, such كـ بيانات ربط أو إنشاء تسلسل. For مثال,theIsReadOnly خاصية هو intended إلى be accessed فقط بواسطة the بيانات-ربط infrastructure بواسطة using a مرجع إلى the ICollection<T> واجهة. The List<T> فئة implements the خاصية بوضوح because it meets this guideline.

Consider implementing واجهة الأعضاء بوضوح إلى simulate الفرق (that هو, تغيير معلمات أو return نوع في overridden الأعضاء).

This هو often done إلى offer strongly-typed versions of the واجهة الأعضاء.

Consider implementing واجهة الأعضاء بوضوح إلى إخفاء a عضو و إضافة an equivalent عضو مع a better اسم.

This effectively renames a عضو. For مثال, Stream implements Dispose بوضوح و provides the Close أسلوب في its place.

Do not استخدم explicit الأعضاء كـ a الأمان boundary.

بوضوح implementing a عضو does not provide أي الأمان. These الأعضاء are publicly callable بواسطة using a مرجع إلى the واجهة.

Do provide a protected ظاهري عضو that offers the same functionality كـ the بوضوح implemented عضو if the functionality هو meant إلى be specialized بواسطة derived فئات.

بوضوح implemented الأعضاء cannot be overridden. If they are redefined في a الفئة المشتقة, it هو impossible for the الفئة المشتقة إلى يتصل the فئة الأساس implementation. You should اسم the protected عضو بواسطة either using the same اسم كـ the explicit واجهة عضو أو affixing Core إلى the واجهة عضو اسم.

أجزاء حقوق النشر 2005 Microsoft Office 2010 Suite Corporation. كافة الحقوق محفوظة.

أجزاء حقوق النشر شركة Addison-Wesley. كافة الحقوق محفوظة.

ل المزيد المعلومات تشغيل إرشادات التصميم، راجع "إطار عمل إرشادات التصميم: كتاب اصطلاحات، Idioms، و نقش لمكتبات.NET القابل لإعادة الاستخدام"ب Krzysztof Cwalina و رفيق Abrams، ينشره Addison-Wesley، 2005.

راجع أيضًا:

المبادئ

تصميم واجهة

موارد أخرى

إرشادات تصميم عضو

تصميم إرشادات لتطوير مكتبات فئة