Monday, February 2, 2015

KB: C#

AppSettingsReader:
Read app.config file

XmlDocument & XmlNode (Read XML file):
XmlDocument configDoc = new XmlDocument();
configDoc.Load(fileName);
XmlNode oneNode = configDoc.SelectSingleNode("//config/");
String abc = botNode.Attributes["proxy"];

Attribute & CustomAttributeData:
FieldInfo[] fs = obj.GetType().GetFields();
foreach (FieldInfo f in fs)
{
IList<CustomAttributeData> attributes = CustomAttributeData.GetCustomAttributes(f);
        foreach (CustomAttributeData attribute in attributes)
        {
             //check if the field was marked as [ConfigValue]
             if (attribute.Constructor.DeclaringType.Equals(typeof(ConfigValue)))
    {}
}
}



    [AttributeUsage(AttributeTargets.Field)]
    public class ConfigValue : Attribute
    {
        private String prameName;
        public ConfigValue(String name)
        {
            prameName = name;
        }

    }

    [AttributeUsage(AttributeTargets.Field)]
    public class RegexFile : Attribute
    {
        private String fileName;
        public RegexFile(String name)
        {
            fileName = name;
        }
    }


Use:
        [ConfigValue]
        String proxy;

        [RegexFile("rabc.regex")]
        private Regex rabc;

No comments:

Post a Comment