Question

public class SomeClass
{


    public SqlParameterCollection SPPC;

    public SomeClass(someType somePrameter)
    {

      ....... SqlParameters assignmet with someParameter goes here .......
        ...                                                              ...
      .......                                                      .......


      foreach (var item in typeof(
                                         GetTimesWithCustomerNames
                                   ).GetMembers()

                )
                {
                    SPPC.Add(item);
                }
     }


        SqlParameter Userid = new SqlParameter();
        SqlParameter Month = new SqlParameter();
        SqlParameter Year = new SqlParameter();
        SqlParameter ReasonId = new SqlParameter();
}

for starters I am here because I am stuck at this point where I need to access sqlParameters that are declared within the body of this class,

those are intended to be added into a collection right after being assigned by the constructor

but as I (hopefully with your help... ) get to solve that issue ,

I wanted to learn a technique that I will stick to , and could probably use, not only with this code in current class, as i will be able to reuse that approach, it should work with most collection . meaning I could systematically add - members/variables or different types of data instead of implicitly/manually put each one , as I could also do with other classes members / fields or properties that are not in same scope as in this example , it makes it even more practical.

my question is

how will you implement this , is using foreach is kind of obsolete as we could use lambda expression over Linq ?

is there other less expensive way to reach the children of a given class not using reflection ?

I will really appreciate an example of implementation

Was it helpful?

Solution

Personally I would do something like this,

public class SomeClass
{
    public List<SqlParameter> SPPC;

    public SomeClass(someType someParameter)
    {
        SPPC = this.GetType().GetMembers().ToList();

        // Or you could restrict it to only fields or properties using Where()
        SPPC = this.GetType().GetMembers().Where(member => member which is of type such).ToList();

        // Or use Select() to get only the value or the name of the members
        SPPC = this.GetType().GetMembers().Select(member => get member value).ToList();

    }
}

You just need to wrap your head around what you need to get from these members as using Reflection can get pretty confusing at times.

Without Reflection

Now, if you don't want to use Reflection, you will need to manually add each on them as you initialize them, as such,

public class SomeClass
{
    public List<SqlParameter> SPPC;

    public SomeClass(someType someParameter)
    {
        SPPC.Add(new SqlParameter());
        SPPC.Add(new SqlParameter());
        SPPC.Add(new SqlParameter());
        ...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top