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
Personally I would do something like this,
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,