मैं नहीं बल्कि प्रकार आप एक कॉन्फ़िग फ़ाइल में एक विशिष्ट मूल्य के लिए दृष्टांत करना चाहते हैं होगा। कुछ की तरह:
<TypeMappings>
< TypeMapping नाम = "जीवन" type = "Entities.LifeEntity, संस्थाओं"/>
< TypeMapping नाम = "संपत्ति" type = "Entities.PropertyEntity, संस्थाओं"/>
< TypeMapping नाम = "विकलांगता" type = "Entities.DisabilityEntity, संस्थाओं"/>
< TypeMapping नाम = "क्रेडिट कार्ड" type = "Entities.CreditCardEntity, संस्थाओं"/>
+०१२३५१६४१०६</TypeMappings >
अपने विधि के अंदर आप तो कॉन्फ़िग फ़ाइल से सभी पंजीकरण निकाल सकते, प्रकार का दृष्टांत के लिए, यदि पंजीकरण नहीं पाया जाता है, तो आप अपवाद फेंक मिलान एक और उपयोग प्रतिबिंब हैं।
namespace Entities
{
public interface IResultEntity
{
}
public class LifeEntity : IResultEntity
{
public override string ToString()
{
return("I'm a Life entity");
}
}
public class PropertyEntity : IResultEntity
{
public override string ToString()
{
return("I'm a Property Entity");
}
}
public class CreditCardEntity : IResultEntity
{
public override string ToString()
{
return("I'm a CreditCard Entity ");
}
}
public class DisabilityEntity : IResultEntity
{
public override string ToString()
{
return("I'm a Disability Entity");
}
}
}
public static Entities.IResultEntity GetEntity(string entityTypeName,string fileName)
{
XDocument doc = XDocument.Load(fileName);
XElement element = doc.Element("TypeMappings").Elements("TypeMapping")
.SingleOrDefault(x => x.Attribute("name").Value == entityTypeName);
if(element == null)
{
throw new InvalidOperationException("No type mapping found for " + entityTypeName);
}
string typeName = element.Attribute("type").Value;
Type type = Type.GetType(typeName);
Entities.IResultEntity resultEntity = Activator.CreateInstance(type) as Entities.IResultEntity;
if(resultEntity == null)
{
throw new InvalidOperationException("type mapping for " + entityTypeName + " is invalid");
}
return resultEntity;
}
public static void Main()
{
try
{
Entities.IResultEntity result = GetEntity("life", @"c:\temp\entities.xml");
Console.WriteLine(result);
result = GetEntity("property", @"c:\temp\entities.xml");
Console.WriteLine(result);
result = GetEntity("disability", @"c:\temp\entities.xml");
Console.WriteLine(result);
result = GetEntity("creditcard", @"c:\temp\entities.xml");
Console.WriteLine(result);
result = GetEntity("foo", @"c:\temp\entities.xml");
Console.WriteLine(result);
}
}
डि चौखटे का एक बहुत आप एक अंतरफलक है कि आप मेटाडाटा के आधार पर क्वेरी कर सकता है के लिए कई पंजीकरण प्रदान करते हैं:
यहां कुछ नमूना कोड है। मेटाडेटा का उपयोग करके एमईएफ निर्यात करता है, इस पर this link देखें।
स्रोत
2009-07-29 01:55:44
का आरक्षित एनम मान होने के साथ ही मैं आमतौर पर एक कारखाने तक पहुंचता हूं, खासकर उन पुस्तकालयों में जहां क्लाइंट ऐप्स अपना स्वयं का कार्यान्वयन जोड़ना चाहते हैं। – Joon
अधिक जानकारी लिंक के लिए वह स्रोत अब उपलब्ध नहीं है, किसी भी मौके पर कहीं और होस्ट किया जा रहा है? या कहीं कहीं समकक्ष पोस्ट है? –
@ सैम हीक मैंने एक संग्रहित संस्करण – Lennart