Tuesday, August 24, 2010

how to get file from resources

Hi,
Today I was busy in two issues
First one was something refactoring I have to deal with some objects and their property name
and object property will be called by name Like I want obj.property(string name).setvalue("")
Any how I achieved this by reflection. lemme me share code with u


private static void LoadPropertiesValues(object dataObject )
{
if(dataObject != null)
{
PropertyInfo[] properties = dataObject.GetType().GetProperties();
foreach (PropertyInfo objInfo in properties)
{
XmlNode xmlMapNode =
_xmlMappingDoc.SelectSingleNode("/parentElement/ChildName[@fieldname='" +
objInfo.Name + "']");
if (xmlMapNode != null && xmlMapNode.Attributes[1] != null &&
xmlMapNode.Attributes[1].Value != string.Empty)
{
string mapGroupName = xmlMapNode.Attributes[1].InnerText;
Mapping mapping = _dataMapperDao.GetMappingByCodeAndResidencyValue(mapGroupName,
dataObject.GetType().GetProperty(objInfo.Name).GetValue(dataObject,null).ToString());
if (mapping != null)
{
dataObject.GetType().GetProperty(objInfo.Name).SetValue(dataObject,mapping.code,null);
}
}

}
}
}


Second issue was access file from resources
Actually I have to load some values from xml file and then I have to take care about its path because in production Envoirnment I cann't guarantee that path will be same neither I can enforce it. It will be changed so I want to get rid of this issue I put this file inside resources, then I put little effort on accessing this file.

Assembly objAssembly = Assembly.GetAssembly(typeof (ClassName));
if(objAssembly!=null)
{
Stream stream = objAssembly.
GetManifestResourceStream("ParentApp.ApplicationName.FileName.xml");
if (stream != null)
{
var xmlTextReader = new XmlTextReader(stream);

_xmlMappingDoc.Load(xmlTextReader);

}
}

So, this is all about today :)

No comments: