Chapter 17 of C# covers Reflection and Attributes, enabling developers to inspect types and assemblies at runtime. It also explores the creation of custom attributes and their usage for code documentation, enhancing the flexibility and maintainability of your code. This chapter is essential for mastering dynamic programming in C# and utilizing metadata for various tasks like debugging and testing.
MCQs on Reflection and Attributes in C#
Section 1: Understanding Reflection (10 Questions)
GetType() method return in C#?
Type object representing the type of the current instanceType.GetProperties() return?
PropertyInfo objectsProperty objectGetMethods()GetFields()GetConstructors()GetProperties()Invoke() method on MethodInfoMethodInfo.Invoke() on TypeMethod class directlyGetFields()GetMethods()GetProperties()GetEvents()Type object?
typeof()GetType()GetAssembly()Assembly.GetType()Section 2: Using Reflection to Inspect Types and Assemblies (10 Questions)
AssemblyTypeTypeInfoReflectionAssembly.GetTypes()Type.GetTypes()Type.GetMethods()Assembly.Load()Assembly allows loading an assembly from a specific file path?
Assembly.LoadFrom()Assembly.Load()Assembly.GetExecutingAssembly()Assembly.LoadFile()Assembly.GetExportedTypes() return?
Assembly object from a type in C#?
type.GetAssembly()Assembly.Load(type)Type.Assembly()type.AssemblyMethodInfo.GetParameters()PropertyInfo.GetParameters()Type.GetMethods()FieldInfo.GetParameters()Assembly.Load()Assembly.GetType()Type.GetAssembly()Assembly.LoadFrom()GetCustomAttributes()GetMethods()GetProperties()GetEvents()Type.GetMethods()Assembly.GetMethods()MethodInfo.GetMethods()TypeInfo.GetMethods()GetMethod(name)GetMethodInfo()GetSpecificMethod()Section 3: Custom Attributes (5 Questions)
AttributeIAttributepublic modifiercustom keywordAttribute suffix in the class namepublic access modifierobjectGetValue()Section 4: Using Attributes for Code Documentation (5 Questions)
DocumentationAttributeXmlDocumentationAttributeObsoleteAttributeSummaryAttributeDeprecatedAttributeObsoleteAttributeDeprecatedMethodAttributeOutdatedAttributeObsolete("This method is outdated.")Obsolete("Use new version")Obsolete("Deprecated")SuppressWarningAttributeCompilerWarningAttributeSuppressMessageAttributeNoWarningAttributepublic class MyClass : Attribute[MyCustomAttribute]public MyCustomAttribute() {}MyCustomAttribute.MyClass()Answer Key
| Qno | Answer |
|---|---|
| 1 | b) A way to inspect and manipulate metadata at runtime |
| 2 | b) System.Reflection |
| 3 | b) A Type object representing the type of the current instance |
| 4 | b) Array of PropertyInfo objects |
| 5 | a) GetMethods() |
| 6 | b) It allows access to private members of types |
| 7 | a) Using Invoke() method on MethodInfo |
| 8 | a) GetFields() |
| 9 | c) GetAssembly() |
| 10 | b) To inspect types and their members dynamically |
| 11 | a) Assembly |
| 12 | a) Using Assembly.GetTypes() |
| 13 | a) Assembly.LoadFrom() |
| 14 | c) Only publicly accessible types |
| 15 | d) type.Assembly |
| 16 | a) MethodInfo.GetParameters() |
| 17 | a) Assembly.Load() |
| 18 | a) GetCustomAttributes() |
| 19 | a) Type.GetMethods() |
| 20 | a) Using GetMethod(name) |
| 21 | a) By inheriting from Attribute |
| 22 | a) A constructor |
| 23 | d) Classes, methods, properties, and fields |
| 24 | b) To define metadata for classes and methods |
| 25 | a) Using Reflection |
| 26 | b) XmlDocumentationAttribute |
| 27 | b) ObsoleteAttribute |
| 28 | d) All of the above |
| 29 | c) SuppressMessageAttribute |
| 30 | b) [MyCustomAttribute] |