Chapter 8 Flashcards
- You are given an assignment to create a code generator to automate the task of creating repetitive code. Which namespace contains the types needed to generate code?
a. System.Reflection
b. CodeDom
c. Reflection
d. System.CodeDom
d. System.CodeDom
- Which code can create a lambda expression?
a. delegate x = x => 5 + 5;
b. delegate MySub(double x);
MySub ms = delegate(double y) {
y * y;
}
c. x => x * x;
d. delegate MySub();
MySub ms = x * x;
c. x => x * x;
- You are consulting for a company called Contoso and are taking over an application that was built by a third-party software company. There is an executable that is currently not working because it is missing a DLL that is referenced. How can you figure out which DLLs the application references?
a. Create an instance of the Assembly class, load the assembly, and iterate through the ReferencedAssemblies property.
b. Create an instance of the Assembly class, load the assembly, and call the GetReferencedAssemblies method.
c. Create an instance of the Assembly class, load the assembly, and iterate through the Modules property.
d. Create an instance of the Assembly class, load the assembly, and call the GetModulesReferenced method.
b. Create an instance of the Assembly class, load the assembly, and call the GetReferencedAssemblies method.
- You are a developer for a finance department and are building a method that uses reflection to get a reference to the type of object that was passed as a parameter. Which syntax can be used to determine an object’s type?
a. Type myType = typeof(myParameter);
b. Object myObject =
myParameter.GetType(object);
c. Object myObject = typeof(myParameter);
d. Type myType = myParameter.GetType();
d. Type myType = myParameter.GetType();
- You are asked to create a custom attribute that has a single property, called Version, that allows the caller to determine the version of a method. Which code can create the attribute?
a. class MyCustomAttribute :
System.Reflection.Attribute
{
public string Version { get; set; }
}
b. class MyCustomAttribute : System.Attribute
{
public string Version { get; set; }
}
c. class MyCustomAttribute :
System.AttributeUsage
{
public string Version { get; set; }
}
d. class MyCustomAttribute :
System.ClassAttribute
{
public string Version { get; set; }
}
b. class MyCustomAttribute : System.Attribute { public string Version { get; set; } }
- Which class in the System.Reflection namespace would you use if you want to determine all the classes contained in a DLL file?
a. FileInfo
b. Assembly
c. Type
d. Module
b. Assembly
- Which method of the Assembly class allows you to get all the public types defined in the Assembly?
a. DefinedTypes
b. Types
c. GetExportedTypes
d. GetModules
c. GetExportedTypes
- Which property of the Assembly class returns the name of the assembly?
a. Name
b. FullyQualifiedName
c. Location
d. FullName
d. FullName
- Which method of the Assembly class returns an instance of the current assembly?
a. GetExecutingAssembly
b. GetAssembly
c. GetCurrentAssembly
d. Assembly
a. GetExecutingAssembly
- Which syntax will Load an Assembly? (Choose all that apply)
a. Assembly.Load(“System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”);
b. Assembly.LoadFrom(@”c:\MyProject\
Project1.dll”);
c. Assembly.LoadFile(@”c:\MyProject\
Project1.dll”);
d. Assembly.ReflectionOnlyLoad((“System.Data,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089”);
a. Assembly.Load(“System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”);
b. Assembly.LoadFrom(@”c:\MyProject\
Project1.dll”);
c. Assembly.LoadFile(@”c:\MyProject\
Project1.dll”);
d. Assembly.ReflectionOnlyLoad((“System.Data,
Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089”);
- Which method should you call if you want the .NET Framework to look in the load-context to load an Assembly?
a. ReflectionOnlyLoad
b. LoadFrom
c. Load
d. LoadFromContext
c. Load
- Which method should you call if you want the .NET Framework to look in the load-from context?
a. ReflectionOnlyLoad
b. LoadFrom
c. Load
d. LoadFromContext
b. LoadFrom
- Which line creates an instance of a DataTable using reflection?
a. myAssembly.CreateInstance
(“System.Data.DataTable”);
b. DataTable.GetType();
c. typeof(“System.Data.DataTable”);
d. myType.CreateInstance
(“System.Data.DataTable”);
a. myAssembly.CreateInstance
(“System.Data.DataTable”);
- Which class would you create if you wanted to determine all the properties contained in a class using reflection?
a. Assembly
b. Class
c. Property
d. Type
d. Type
- How can you determine if a class is public or private?
a. Create an instance of the Type class using the typeof keyword and then examine the IsPublic property of the Type variable.
b. Create an instance of the Type class using the typeof keyword and then examine the IsPrivate property of the Type variable.
c. Create an instance of the class within a try catch block and catch the exception if you cannot create an instance of the class.
d. Create an instance of the class and check the IsPublic property.
a. Create an instance of the Type class using the typeof keyword and then examine the IsPublic property of the Type variable.
- Which class in the System.Reflection namespace is used to represent a field defined in a class?
a. PropertyInfo
b. FieldInfo
c. Type
d. EventInfo
b. FieldInfo