Create Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shapper

    #1

    Create Method

    Hello,

    I have the following code:

        MySection sect =
    System.Configur ation.Configura tionManager.Get Section(Name) as
    MySection;
        foreach (Person p in sect.People) {
            Interaction.Msg Box(p.Name);
        }

    Now I am trying to create a method that is a little bit more generic.

    As result as GetSection I will get a class. This class can be of many
    types (MySection, Google, ...).
    All I know is that this class inherits ConfigurationEl ementCollection .

    Then I want to loop through each item of this class and find the item
    which Name is equal to a Key value I provide.
    The item can be of type People (if the collection is of type
    MySection), Service (if the collection if of type Google), ...

    The one thing I know is that any of this classes has a property named
    Name to which I will compare to Key.

    I would like to create a method for this, for example Get, and then
    use it:

    People p = (People)Get("My Section")

    or

    Service s = (Service)Get("G oogle")

    Is this possible to create this generalization?

    Or should I even do this?

    Thanks,
    Miguel


  • shapper

    #2
    Re: Create Method

    On Oct 3, 1:41 am, shapper <mdmo...@gmail. comwrote:
    Hello,
    >
    I have the following code:
    >
        MySection sect =
    System.Configur ation.Configura tionManager.Get Section(Name) as
    MySection;
        foreach (Person p in sect.People) {
            Interaction.Msg Box(p.Name);
        }
    >
    Now I am trying to create a method that is a little bit more generic.
    >
    As result as GetSection I will get a class. This class can be of many
    types (MySection, Google, ...).
    All I know is that this class inherits ConfigurationEl ementCollection .
    >
    Then I want to loop through each item of this class and find the item
    which Name is equal to a Key value I provide.
    The item can be of type People (if the collection is of type
    MySection), Service (if the collection if of type Google), ...
    >
    The one thing I know is that any of this classes has a property named
    Name to which I will compare to Key.
    >
    I would like to create a method for this, for example Get, and then
    use it:
    >
    People p = (People)Get("My Section")
    >
    or
    >
    Service s = (Service)Get("G oogle")
    >
    Is this possible to create this generalization?
    >
    Or should I even do this?
    >
    Thanks,
    Miguel
    I am posting the entire code so it is better to explain:

    Config

    public class Config : ConfigurationSe ction {

    [ConfigurationPr operty("Google" )]
    public GoogleCollectio n Google {
    get { return this["Google"] as GoogleCollectio n; }
    }
    [ConfigurationPr operty("Mail")]
    public MailCollection Mail {
    get { return this["Mail"] as MailCollection; }
    }

    public static object Get(string section, ConfigType type, string
    key) {
    object parent = ConfigurationMa nager.GetSectio n(section);
    // ?????
    }

    } // Config

    GoogleCollectio n

    public class GoogleCollectio n : ConfigurationEl ementCollection {

    protected override ConfigurationEl ement CreateNewElemen t() {
    return new Google();
    }
    protected override object GetElementKey(C onfigurationEle ment
    element) {
    return (element as Google).Key;
    }

    }

    Google

    public class Google : ConfigurationEl ement {

    [ConfigurationPr operty("Key", IsRequired = true)]
    public string Access { get; set; }
    public string Key { get; set; }
    public string Type { get; set; }

    }

    These are configuration classes to create custom configuration section
    in Web.Config.

    As you can see in Config I can have many subsections, Google, Mail,
    etc ...

    The Web.Config looks as follows:

    <MyApp>
    <Google>
    <add key ="AdSense" type ="Client" access = "" />
    <add key ="Verify" type ="Id" access = "" />
    </Google>
    <Mail>
    </Mail>
    </MyApp>

    So basically what I am trying to create is a method in Config that
    given a section (Ex: MyApp) a type (Ex: ConfigType.Goog le) and a Key
    (Ex: AdSense) I would get that part of the Web.Config.

    In this case would be:

    <add key ="AdSense" type ="Client" access = "" />

    And this is an instance of Google.

    I would like to have the get method inside Config ... this is my
    problem.

    Any idea?

    Thanks,

    Miguel

    Comment

    Working...