Reputation: 4511
I want to create column validator where I can pass custom functions/methods.
Edit: I don't need to dynamically build these functions it is possible to build solution before hand, and I am constrained to .net 3.5. Basically I'm trying to create reusable class for my SSIS Script processes.
I did not know how to do that in C# so I created a fast representation in JavaScript of what I want to achieve here
// Data extracted from file
var data = "2|User2|15\r\n3|User1|13";
// Define columns in file
var columnKeys = ["ID", "Username", "Age"];
// Custome column validation
var columnValidation = {
"ID": function (value) {
if (isNaN(value)) return false;
else return true;
},
"Username": function (value) {
return true;
},
"Age": function (value) {
return true;
}
};
var ValidateColumns = function (data, keys, rules) {
var rowSep = "\r\n";
var colSep = "|";
// Get rows
var rows = data.split(rowSep);
_.each(rows, function (row) {
// Get columns
var columns = row.split(colSep);
// Validate columns
_.each(columns, function (column) {
if (rules[keys[columns.indexOf(column)]](column)) {
console.log("Column " + keys[columns.indexOf(column)] + ": Passed");
}
else {
console.log("Column " + keys[columns.indexOf(column)] + ": Failed");
}
});
});
};
ValidateColumns(data, columnKeys, columnValidation);
Questions:
Upvotes: 0
Views: 206
Reputation: 843
You should read about the Func and Actions
If you function return something (like a bool!) and/or uses multiple parameters, you should use Func<TReturn,TParam1,TParam2>[]
and use as many TParam as you have parameters. There is alot of functionnality behind these so I recommend you to read about it. This link seems good : Explanation of Func
If your functions do not take any parameters and return void (which is not the case in your example, sorry), you could use a Action[] which is used like this :
Action action = () => //your function's code;
//Lame example of using Action[]
YourMethodThatTakesFuncAsParams(new Action[]{action,action,action});
If all of your functions return something different and/or uses different parameters, I don't think there is a clean solution to achieve this.
Little edit (since I can't comment on stuff!) I think Ben's answer is the most appropriate and direct approach to your problem
Upvotes: 2
Reputation: 1080
What you are looking for is a predicate. Essentially it represents a function with a definable input type and a boolean output.
Here is an example of passing an array of Predicates.
//this is a Predicate<string> b/c it takes a string as input and outputs a boolean
bool ExamplePredicate(string input)
{
return input == "something";
}
//function accepting an array of Predicate<string> as input
bool Validate(Predicate<string>[] validators /*, other data input*/ )
{
//use the array of validators to process your data.
}
void Test()
{
//array of validators
var validators = new Predicate<string>[]
{
aString => !string.IsNullOrEmpty(aString), // you can use lambda expressions
ExamplePredicate // or reference functions matching the predicate definitions
}
Validate(validators);
}
Documentation: http://msdn.microsoft.com/en-us/library/bfcke1bz(v=vs.110).aspx
Upvotes: 1
Reputation: 76426
You are looking for delegates. Please read the MSDN documentation. I can advise you these particular links:
http://msdn.microsoft.com/en-us/library/aa288459%28v=vs.71%29.aspx
http://msdn.microsoft.com/en-us/library/ms173172.aspx
The first is a tutorial about delegates and the second is about using them.
Upvotes: 0