In an inherited class with managed resources C# with Example



In an inherited class with managed resources C# with Example

It's fairly common that you may create a class that implements IDisposable, and then derive classes that also 
contain managed resources. It is recommendeded to mark the Dispose method with the virtual keyword so that 
clients have the ability to cleanup any resources they may own. 
public class Parent : IDisposable 
{ 
private ManagedResource parentManagedResource = new ManagedResource(); 
public virtual void Dispose() 
{ 
if (parentManagedResource != null) 
{ 
parentManagedResource.Dispose(); 
} 
} 
} 
public class Child : Parent 
{ 
private ManagedResource childManagedResource = new ManagedResource(); 
public override void Dispose() 
{ 
if (childManagedResource != null) 
{ 
childManagedResource.Dispose(); 
} 
//clean up the parent's resources 
base.Dispose(); 
} 
} 
 

Reflection is a C# language mechanism for accessing dynamic object properties on runtime. Typically, reflection is 
used to fetch the information about dynamic object type and object attribute values. In REST application, for 
example, reflection could be used to iterate through serialized response object. 
Remark: According to MS guidelines performance critical code should avoid reflection. See 
https://msdn.microsoft.com/en-us/library/ff647790.aspx 

0 Comment's

Comment Form

Submit Comment