public Security ConvertToSecurityObject() { Security secObj = null; if(SecurityTypeName == typeof(Equity).Name) { secObj = new Equity(); } else if(SecurityTypeName == typeof(CorporateBond).Name) { secObj = new CorporateBond(); } foreach(var tab in TabList) { foreach(var att in tab.Attributes) { Type propertyType = secObj.GetType().GetProperty(att.AttributeRealName).PropertyType; //Following if part is to handle conversion of nullable types (eg. int?) if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { propertyType = Nullable.GetUnderlyingType(propertyType); } //TODO: Find a way to convert the date strings from excel into SqlDateTime properly. Below statement is temporarily placed to avoid using the Date attributes. if (propertyType == typeof(SqlDateTime)) continue; if(att.AttributeValue != null) secObj.GetType().GetProperty(att.AttributeRealName).SetValue( secObj, Convert.ChangeType(att.AttributeValue, propertyType), null ); } } return secObj; }
Security GetSecurityObject(string securityName) { Security security = null; switch(securityName) { case "Equities": security = new Equity(); break; case "Bonds": security = new CorporateBond(); break; } return security; }