Reputation: 495
I have a list of methods I am calling in a list:
List<ReportResult> results = new List<ReportResult>() {
await GetInvalid("ASC", offset, limit),
await GetActive("ASC", offset, limit),
await GetValid("ASC", offset, limit)
};
When iterating through each item in the list I'd like to set a string equal to the name of the method that was called. For example:
DataTable outTable = new DataTable();
foreach (ReportResult report in results)
{
DataRow dr = outTable.NewRow();
string methodName = "GetInvalid";
dr[0] = methodName;
outTable.Rows.Add(dr);
}
Upvotes: 0
Views: 122
Reputation: 52240
If the items in the list each have a different meaning (e.g. need to be interpreted in context with the method name), I would suggest they don't belong in a list at all.
The way I would do it is:
var invalid = await GetInvalid("ASC", offset, limit);
var active = await GetActive("ASC", offset, limit);
var valid = await GetValid("ASC", offset, limit);
Since they are in separate fields, it is obvious where the results come from.
If the results need to be logically grouped, put them in a class or struct.
class Results
{
public ReportResult Invalid { get; set; }
public ReportResult Valid { get; set; }
public ReportResult Active { get; set; }
}
If you both need them to be used different AND need them in a list, do the above, but supplement it with a list.
var invalid = await GetInvalid("ASC", offset, limit);
var active = await GetActive("ASC", offset, limit);
var valid = await GetValid("ASC", offset, limit);
var results = new List<ReportResult>
{
invalid, active, valid
};
Or
class Results
{
public ReportResult Invalid { get; set; }
public ReportResult Valid { get; set; }
public ReportResult Active { get; set; }
public List<ReportResult> AllResults => new List<ReportResult>{Invalid,Valid,Active};
}
Upvotes: 0
Reputation: 35017
Inside all three methods you could call a GetName method and add a new field to ReportResult.
GetName would look something like this:
public static string GetName([CallerMemberName]string memberName = "")
=> memberName;
class ReportResult {
//(...)
public string Mama {get;set;} // Aka Source
}
public static string GetCaller([CallerMemberName]string memberName = "") => memberName;
public async static Task Main()
{
var offset = 1;
var limit = 10;
List<ReportResult> results = new List<ReportResult>() {
await GetInvalid("ASC", offset, limit),
await GetActive("ASC", offset, limit),
await GetValid("ASC", offset, limit)
};
foreach(var r in results ) Console.WriteLine(r.Mama);
}
private static Task<ReportResult> GetValid(string v, object offset, object limit) {
return Task.FromResult(new ReportResult{ Mama = GetCaller() });
}
private static Task<ReportResult> GetActive(string v, object offset, object limit) {
return Task.FromResult(new ReportResult{ Mama = GetCaller() });
}
private static Task<ReportResult> GetInvalid(string v, object offset, object limit) {
return Task.FromResult(new ReportResult{ Mama = GetCaller() });
}
GetInvalid
GetActive
GetValid
Upvotes: 2
Reputation: 109
When you're using
await GetInvalid("ASC", offset, limit)
you work with values, not methods. .NET provides System.Reflection to work with methods and System.Threading.Tasks.Task to work with functions. If you're interested in method names, it's better to use first variant.
var thisType = this.GetType();
var resultsMethods = new List<MethodInfo>() {
thisType.GetMethod("GetInvalid"),
thisType.GetMethod("GetActive"),
thisType.GetMethod("GetValid")
};
And of course you should define the methods. So far you can get names:
List<string> resultNames = resultsMethods.Select(method => method.Name);
List<ReportResult> results = resultsMethods.Select(method =>
Invoke(this, new object ["ASC", offset, limit]));
Upvotes: 0
Reputation: 4219
You could use the nameof
operator to pass the name of the method being called along with the result of the method as a Tuple in your List:
List<(string, string)> results = new List<(string, string)>
{
(nameof(GetInvalid), GetInvalid()),
(nameof(GetActive), GetActive()),
(nameof(GetValid), GetValid()),
};
foreach ((string MethodName, string Result) in results)
{
Console.WriteLine($"Method: {MethodName}, Result: {Result}");
}
This yields the following results:
Upvotes: 0
Reputation: 3880
In my opinion the easiest solution will just be to do something like that:
List<KeyValuePair<string,ReportResult>> results = new List<KeyValuePair<string,ReportResult>> () {
new KeyValuePair<string, ReportResult>("GetInvalid", await GetInvalid("ASC", offset, limit)),
new KeyValuePair<string, ReportResult>("GetActive", await GetActive("ASC", offset, limit)),
new KeyValuePair<string, ReportResult>("GetValid", await GetValid("ASC", offset, limit))
};
a more clean solution will be just to use Dictonary instead of a list of KeyValuePair, but this way you can have the same key more then once.
Upvotes: 2