Create Method

S

shapper

Hello,

I have the following code:

    MySection sect =
System.Configuration.ConfigurationManager.GetSection(Name) as
MySection;
    foreach (Person p in sect.People) {
        Interaction.MsgBox(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 ConfigurationElementCollection.

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("MySection")

or

Service s = (Service)Get("Google")

Is this possible to create this generalization?

Or should I even do this?

Thanks,
Miguel
 
S

shapper

Hello,

I have the following code:

    MySection sect =
System.Configuration.ConfigurationManager.GetSection(Name) as
MySection;
    foreach (Person p in sect.People) {
        Interaction.MsgBox(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 ConfigurationElementCollection.

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("MySection")

or

Service s = (Service)Get("Google")

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 : ConfigurationSection {

[ConfigurationProperty("Google")]
public GoogleCollection Google {
get { return this["Google"] as GoogleCollection; }
}
[ConfigurationProperty("Mail")]
public MailCollection Mail {
get { return this["Mail"] as MailCollection; }
}

public static object Get(string section, ConfigType type, string
key) {
object parent = ConfigurationManager.GetSection(section);
// ?????
}

} // Config

GoogleCollection

public class GoogleCollection : ConfigurationElementCollection {

protected override ConfigurationElement CreateNewElement() {
return new Google();
}
protected override object GetElementKey(ConfigurationElement
element) {
return (element as Google).Key;
}

}

Google

public class Google : ConfigurationElement {

[ConfigurationProperty("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.Google) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top