Documente Academic
Documente Profesional
Documente Cultură
NET
.NET Framework 4.5 New Features Interview
Questions
2013-09-27 07:09:14 Jinal Desai
fr-FR
fr-FR
0,78
0,80
0,37
fr-FR
fr-FR
0,52
0,32
0,15
Unicode 5.0
Unicode 5.0
Unicode 6.0
regions = GenerateData();
ver = CultureInfo.CurrentCulture.CompareInfo.Version;
reindex = true;
}
// Retrieve the existing data.
else {
regions = RestoreData(out ver);
}
// Determine whether the current ordering is valid; if not, reorder.
if (reindex || ver != CultureInfo.CurrentCulture.CompareInfo.Version) {
Array.Sort(regions, new Example());
// Save newly reordered data.
SaveData(regions);
}
// Continue with application...
}
private static Region[] GenerateData()
{
List<Region> regions = new List<Region>();
foreach (var culture in CultureInfo.GetCultures(CultureTypes.AllCultures)) {
if (culture.IsNeutralCulture | culture.Equals(CultureInfo.InvariantCulture))
continue;
RegionInfo region = new RegionInfo(culture.Name);
regions.Add(new Region(region.Name, region.NativeName));
}
return regions.ToArray();
}
private static Region[] RestoreData(out SortVersion ver)
{
List<Region> regions = new List<Region>();
BinaryReader binaryReader = new BinaryReader(File.Open(FILENAME, FileMode.Open));
int sortVer = binaryReader.ReadInt32();
Guid sortId = Guid.Parse(binaryReader.ReadString());
ver = new SortVersion(sortVer, sortId);
string id, name;
while (binaryReader.PeekChar() != -1) {
id = binaryReader.ReadString();
name = binaryReader.ReadString();
regions.Add(new Region(id, name));
}
return regions.ToArray();
}
private static void SaveData(Region[] regions)
{
SortVersion sortVersion = CultureInfo.CurrentCulture.CompareInfo.Version;
BinaryWriter binaryWriter = new BinaryWriter(File.Open(FILENAME, FileMode.Create));
binaryWriter.Write(sortVersion.FullVersion);
binaryWriter.Write(sortVersion.SortId.ToString());
foreach (var region in regions) {
binaryWriter.Write(region.Id);
binaryWriter.Write(region.NativeName);
}
binaryWriter.Close();
}
public int Compare(object o1, object o2)
{
// Assume that all casts succeed.
Region r1 = (Region) o1;
Region r2 = (Region) o2;
return String.Compare(r1.NativeName, r2.NativeName,
StringComparison.CurrentCulture);
}
}
10. How to customize a reflection context to override default reflection
behavior?
Ans. We can achieve this using CustomReflectionContext class. provides a way for
you to add or remove custom attributes from reflection objects, or add dummy
properties to those objects, without re-implementing the complete reflection model.
The default CustomReflectionContext simply wraps reflection objects without
making any changes, but by subclassing and overriding the relevant methods, you
can add, remove, or change the attributes that apply to any reflected parameter or
member, or add new properties to a reflected type.
For example, suppose the code follows the convention of applying a particular
attribute to factory methods, but we are now required to work with third-party code
that lacks attributes. We can use CustomReflectionContext to specify a rule for
identifying the objects that should have attributes and to supply the objects with
those attributes when they are viewed from our code.
Note: To use CustomReflectionContext effectively, the code that uses the reflected
objects must support the notion of specifying a reflection context, instead of
assuming that all reflected objects are associated with the runtime reflection
context. Many reflection methods in the .NET Framework provide a
ReflectionContext parameter for this purpose.
To modify the attributes that are applied to a reflected parameter or member,
override the GetCustomAttributes(ParameterInfo, IEnumerable<Object>) or
GetCustomAttributes(MemberInfo, IEnumerable<Object>) method. These methods
take the reflected object and the list of attributes under its current reflection context,
and return the list of attributes it should have under the custom reflection context.
//A blank example attribute.
class myAttribute : Attribute
{
}
//Reflection context with custom rules.
class myCRC : CustomReflectionContext
{
//Called whenever the reflection context checks for custom attributes.
protected override IEnumerable<object> GetCustomAttributes(MemberInfo member,
IEnumerable<object> declaredAttributes)
{
//Add example attribute to "To*" members.
if (member.Name.StartsWith("To")) {
yield return new myAttribute();
}
//Keep existing attributes as well.
foreach (var attr in declaredAttributes) yield return attr;
}
}
class Program
{
static void Main(string[] args)
{
myCRC mc = new myCRC();
Type t = typeof(String);
//A representation of the type in the default reflection context.
TypeInfo ti = t.GetTypeInfo();
//A representation of the type in the customized reflection context.
TypeInfo myTI = mc.MapType(ti);
//Display all the members of the type and their attributes.
foreach (MemberInfo m in myTI.DeclaredMembers)
{
Console.WriteLine(m.Name + ":");
foreach (Attribute cd in m.GetCustomAttributes())
{
Console.WriteLine(cd.GetType());
}
}
Console.WriteLine();
//The "ToString" member as represented in the default reflection context.
MemberInfo mi1 = ti.GetDeclaredMethods("ToString").FirstOrDefault();
//All the attributes of "ToString" in the default reflection context.
Console.WriteLine("'ToString' Attributes in Default Reflection Context:");
return s;
}
public void ShowStringHashCode()
{
Console.WriteLine("String '{0}' in domain '{1}': {2:X8}",
s, AppDomain.CurrentDomain.FriendlyName,
s.GetHashCode());
}
}
//Output
//String
//String
//Output
//String
//String
5/5
1 vote, 5.00 avg. rating (96% score)