In this example I want to add a .loop({quantity},{sleepvalue}) to a method
I got it to work with this:
this.loop(count, 500,
()=>{
var image = Screenshots.getScreenshotOfDesktop();
pictureBox.load(image);
images.Add(image);
updateTrackBar();
});
using this extension method:
public static void loop(this Object _object, int count, int delay, MethodInvoker methodInvoker)
{
for(int i=0; i < count; i++)
{
methodInvoker();
_object.sleep(delay);
}
}
which means that the invocation syntax is:
this.loop(15,500, () => {...code...});
but ideally what I wanted to do was something like:
()=> { ...code...}.loop(10,500);
which doesn't work unless I do it like this:
new MethodInvoker(()=>{...code...}).loop(10,500);
which will work with this version of the extension method:
public static void loop(this MethodInvoker methodInvoker, int count, int delay)
{
for(int i=0; i < count; i++)
{
methodInvoker();
Processes.Sleep(delay);
}
}
No, unfortunately there isn't.
I blogged about this quite a while ago :(
However, with the right choice of indentation, you can make it look almost identical to a normal loop:
HelperType.Loop(count, 500, () =>
{
// Code here
});
That's what a lot of the Parallel Extensions samples look like
I wouldn't make it an extension method on something you're not actually going to use, just so that you can use "this" instead of the real type name... not unless you've got a real use for the object it's called "on", anyway.
(I'd also suggest following .NET naming conventions - make your methods PascalCase.)
I really advise not to try to do this.
It's often a bad idea, in my opinion, to implement extension methods on core types, such as a delegate/lambda type, or System.Object.
This just causes confusion. I see little advantage to your proposed syntax:
() => { ..code... }.Loop(10, 500);
To me, that is far less readable than:
Utilities.RepeatWithDelay(10, 500, () => {....code... } );
Related
I'm generating a random number from 1-1000. I have 200 functions named function1, function4, function 10, function 11, etc. What I would like to do is execute a specific function depending on if the number generated requires a function, and ignore it if not.
My first thought was to create an int[] containing all of the values that would trigger a function, and if the int[] contains the random number to use if statements to figure out what the number is. I'm concerned that it must be a really crude solution to an easy problem though.
I know the "best way" to do something is subjective, but is there a better way to accomplish this?
UPDATE: As per comments, I should probably have started out by pointing out that doing this for 200 functions is probably a good sign that there is some serious issue in your design. This is probably an XY question where you are trying to solve a problem in some crazy way and asking about your intended solution instead of asking about the problem itself.
That said I'll leave the original answer because it's still good advice when mapping a reasonable amount of function calls that can/will change during the life cylce of your app or dynamically as the code runs.
I won't get into why you are doing this, but I'll try to at least point you in the right direction so this doesn't become a complete nightmare when you need to modify/expand behavior:
You can map numbers to function calls using delegates and a dictionary. Assuming your functions take no arguments and return void you'd do:
var functionsMap = new Dictionary<int, Action>();
//map functions
var r = getSomeRandomNumber();
if (functions.TryGetValue(r), out var a)
a(); //invoke function
Mapping functions is simply adding keys and values:
functionsMap.Add(1, () => function1());
functionsMap.Add(3, () => function3());
//etc.
If your functions take arguments or return values, you'd use the adequate delegate: Action<T>, Func<T1, T2> etc.
You can use reflection to invoke appropriate method:
Type exampleType = exampleObject.GetType();
MethodInfo exampleMethod = exampleType.GetMethod(methodName);
exampleMethod.Invoke(this, null);
Where methodName can be created using your random number.
Without commenting on the wisdom of having 200 functions named the way yours are, you can use reflection to determine whether a given functionX() exists, like so:
public void ExecuteDynamicMethod(int number)
{
// Modify these two lines with your app's dll/exe and class type:
Assembly assembly = Assembly.LoadFile("...Assembly1.dll");
Type type = assembly.GetType("YourClassType");
if (type != null)
{
MethodInfo methodInfo = type.GetMethod("function" + number);
if (methodInfo != null)
{
object classInstance = Activator.CreateInstance(type, null);
methodInfo.Invoke(classInstance, null); // null = "no function arguments"
}
}
}
This can then be called for a given value like
ExecuteDynamicMethod(14);
See this SO answer for the inspiration behind this.
Reflection can be used for this purpose. I want to give and keep below example for not only the objective of the question but also for future reference. Also, of course that many function is not good but below code shows the approach that can work with many functions if they have similar name (like starting with "function" keyword).
Assume below is Methods.cs
using System;
using System.Reflection;
namespace YourMethodNamespace
{
public class YourMethodClass
{
public void function1()
{
Console.WriteLine("Function-1");
}
public void function2()
{
Console.WriteLine("Function-2");
}
...
public void function200()
{
Console.WriteLine("Function-200");
}
public static void invokeMethodsDynamically(int randomNumber){
Type yourClassType = typeof(YourMethodClass);
ConstructorInfo yourClassConstructorInfo = yourClassType.GetConstructor(Type.EmptyTypes);
object yourClassObject = yourClassConstructorInfo.Invoke(new object[]{});
//If the constructor has parameters, then we can pass them by this way. Like below;
/*ConstructorInfo yourClassConstructorInfo = yourClassType.GetConstructor(new[]{typeof(int)});
object yourClassObject = yourClassConstructorInfo.Invoke(new object[]{3});
*/
MethodInfo[] methodInfoArr = yourClassType.GetMethods();
foreach(MethodInfo methodInfo in methodInfoArr){
if(methodInfo.Name == "function" + randomNumber){
methodInfo.Invoke(yourClassObject, null);
}
}
}
}
}
Let's say below is Program.cs
using System;
using YourMethodNamespace;
namespace YourProgramNamespace
{
public class YourProgramClass
{
public static void Main()
{
Random random = new Random();
int randomNumber = random.Next(1, 201);
//If Methods.cs is in another Assembly
/*string pathToDllAssembly = #"Domain.dll";
Assembly dllAssembly = Assembly.LoadFrom(pathToDllAssembly);
Type methodsClassType = dllAssembly.GetType("YourMethodNamespace.YourMethodClass");
ConstructorInfo methodClassConstructorInfo = methodsClassType.GetConstructor(Type.EmptyTypes);
object methodsClassObject = methodClassConstructorInfo.Invoke(new object[]{});
MethodInfo methodInfo = methodsClassType.GetMethod("invokeMethodsDynamically");
methodInfo.Invoke(methodsClassObject, new object[]{randomNumber});
*/
YourMethodClass.invokeMethodsDynamically(randomNumber, null);
}
}
}
Also for testing and observing, below link can be used.
https://repl.it/#erdsavasci/ReflectionTest
I'm essentially a VB.Net programmer who's having to do a small side-project in c#, so please forgive me if this is an obvious question!
I am using a third party API that returns data in array, for which I have to interrogate the datatype before obtaining the data, and then pass to the API an array of the correct data type in order for the data to be packed correctly: e.g.
// Get info
Int32 dataLength = MyAPI.GetDataLength();
Int32 dataDepth = MyAPI.GetDataDepth();
//And get the data.
switch (dataDepth)
{
case 8:
byte[] bData = new byte[dataLength];
MyAPI.GetData(bData);
//Do Work
WorkFunction(bData);
break;
case 16:
Int16[] iData = new Int16[dataLength];
MyAPI.GetData(iData);
//Do Work
WorkFunction(iData);
break;
}
This works fine, and correctly populates either the byte array, or the Int16 array. At the moment, I have an overload for WorkFunction as follows:
void WorkFunction(byte[] data){
//Do Stuff
}
void WorkFunction(Int16[] data){
//Do Stuff
}
This is fine, and all works OK - but the work performed in WorkFunction is essentially the same (displaying text/graphing data) and having what is essentially a copy-and-paste of code in two functions can't be great practice?
I realise I could just have the Int16[] version of WorkFunction and convert
int[] bytesAsInts = yourBytes.Select(x => (int)x).ToArray();
but the data is being fed in from a hardware device, and I am reluctant to put in additional "work" unless it's actually required.
What would you consider is "best practice" ??
Thanks.
Thanks for comments so far. WorkFunction is pretty simple. It's updating a few
labels, and plotting some points on a graph. There are a couple of extra parameters, but these are of fixed data type:
void WorkFunction(Int16[] data, double aValue){
//Do Stuff
MyTextBox.text = aValue.ToString("0.00");
MyChartSeries.Points.Clear();
for (int i = 0; i < data.Length; i++) {
MyChartSeries.Points.AddXY(i, data[i]); }
}
I would use generics as explained in the comments, but I think you need an answer in order to close your question.
void WorkFunction<DType>(DType[] data, double aValue){
//Do Stuff
MyTextBox.text = aValue.ToString("0.00");
MyChartSeries.Points.Clear();
for (int i = 0; i < data.Length; i++) {
MyChartSeries.Points.AddXY(i, data[i]); }
}
Generics will do the job for you.
If you want to know more about generics maybe with a more familiar language, check this.
Then if the implementation doesn't fit the purpose, please tell us more about the usage of data.
Overloading is good practice when functionality is same in c#.net. but recently few years/months there are also come with new features like generic method which will allow creating single method with generic arguments. so you have the option not to create overloading but create the generic function which will allow all dataype in just single function
example:
void WorkFunction<Datatype>(Datatype[] data, double aValue){
//Do Stuff
MyTextBox.text = aValue.ToString("0.00");
MyChartSeries.Points.Clear();
for (int i = 0; i < data.Length; i++) {
MyChartSeries.Points.AddXY(i, data[i]);
}
}
One option i often use because of it's simplicity is to put "identical code" inside a separate function :
void WorkFunction(byte[] data){
int[] bytesAsInts = data.Select(x => (int)x).ToArray();
DoStuff(bytesAsInts);
}
void WorkFunction(Int16[] data){
DoStuff(data);
}
public void DoStuff(int16[] dataFromBoth)
{
// here is where you do stuff, whatever it came from.
}
It's basically code extracting, there is even some tools that can do it for you accurately.
I created a library that handles database access. I recently added transaction handling; however, I came across a small issue. To outline this, I wrote this sample for demonstration purposes:
class Program
{
static void Main(string[] args)
{
String data = null;
DoAction(ref data, () =>
{
Console.WriteLine(data);
});
Console.ReadLine();
}
private static void DoAction(ref String data, Action action)
{
if (data == null)
data = "Initialized Data";
action();
}
}
I get "Access to modified closure" underline on the following code line for the 'data' variable:
Console.WriteLine(data);
I understand that the modification of the ref data variable can cause issues (e.g. when running foreach loops). However, in the following case, I don't see this to happen.
Here is another version with a loop changing the variable further - the output is as expected:
class Program
{
static void Main(string[] args)
{
String data = null;
for (var i = 0; i < 10; i++)
DoAction(ref data, () =>
{
Console.WriteLine(data);
});
Console.ReadLine();
}
private static void DoAction(ref String data, Action action)
{
if (data == null)
data = "Initialized Data";
else
data += "|";
action();
}
}
ReSharper offers me to create a local variable, but I explicitly want to use the created string from the DoAction() method. If I would accept ReSharpers approach, it actually would break the code. Is there any other way to solve this problem? I'd like to use this Action approach, but I don't want ReSharper to complain about it either (and possibly not disable ReSharpers inspection).
Any suggestions?
I would suggest avoid using a ref parameter for this in the first place - it seems needlessly complicated to me. I'd rewrite DoAction as:
static string DoAction(string data, Action<string> action)
{
data = data == null ? "Initialized Data" : data + "|";
action(data);
return data;
}
Then you can have:
data = DoAction(data, Console.WriteLine);
or if you want to use a lambda expression:
data = DoAction(data, txt => Console.WriteLine(txt));
You can make DoAction a void method if you don't actually need the result afterwards. (It's not clear why you need the result to be returned and a delegate to execute in DoAction, but presumably that makes more sense in your wider context.)
In case you feel certain that the warning is not appropriate, there is the InstantHandleAttribute which is documented as:
Tells code analysis engine if the parameter is completely handled
when the invoked method is on stack. If the parameter is a delegate,
indicates that delegate is executed while the method is executed.
If the parameter is an enumerable, indicates that it is enumerated
while the method is executed.
I think is exactly what you want.
You can get the attribute from the JetBrains.Annotations package or alternatively as copy-paste from ReSharper options.
I'm writing HFT trading software. I do care about every single microsecond. Now it written on C# but i will migrate to C++ soon.
Let's consider such code
// Original
class Foo {
....
// method is called from one thread only so no need to be thread-safe
public void FrequentlyCalledMethod() {
var actions = new List<Action>();
for (int i = 0; i < 10; i++) {
actions.Add(new Action(....));
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
I guess that ultra-low latency software should not use "new" keyword too much, so I moved actions to be a field:
// Version 1
class Foo {
....
private List<Action> actions = new List<Action>();
// method is called from one thread only so no need to be thread-safe
public void FrequentlyCalledMethod() {
actions.Clear()
for (int i = 0; i < 10; i++) {
actions.Add(new Action { type = ActionType.AddOrder; price = 100 + i; });
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
And probably I should try to avoid "new" keyword at all? I can use some "pool" of pre-allocated objects:
// Version 2
class Foo {
....
private List<Action> actions = new List<Action>();
private Action[] actionPool = new Action[10];
// method is called from one thread only so no need to be thread-safe
public void FrequentlyCalledMethod() {
actions.Clear()
for (int i = 0; i < 10; i++) {
var action = actionsPool[i];
action.type = ActionType.AddOrder;
action.price = 100 + i;
actions.Add(action);
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
How far should I go?
How important to avoid new?
Will I win anything while using preallocated object which I only need to configure? (set type and price in example above)
Please note that this is ultra-low latency so let's assume that performance is preferred against readability maintainability etc. etc.
In C++ you don't need new to create an object that has limited scope.
void FrequentlyCalledMethod()
{
std::vector<Action> actions;
actions.reserve( 10 );
for (int i = 0; i < 10; i++)
{
actions.push_back( Action(....) );
}
// use actions, synchronous
executor.Execute(actions);
// now actions can be deleted
}
If Action is a base class and the actual types you have are of a derived class, you will need a pointer or smart pointer and new here. But no need if Action is a concrete type and all the elements will be of this type, and if this type is default-constructible, copyable and assignable.
In general though, it is highly unlikely that your performance benefits will come from not using new. It is just good practice here in C++ to use local function scope when that is the scope of your object. This is because in C++ you have to take more care of resource management, and that is done with a technique known as "RAII" - which essentially means taking care of how a resource will be deleted (through a destructor of an object) at the point of allocation.
High performance is more likely to come about through:
proper use of algorithms
proper parallel-processing and synchronisation techniques
effective caching and lazy evaluation.
As much as I detest HFT, I'm going to tell you how to get maximum performance out of each thread on a given piece of iron.
Here's an explanation of an example where a program as originally written was made 730 times faster.
You do it in stages. At each stage, you find something that takes a good percentage of time, and you fix it.
The keyword is find, as opposed to guess.
Too many people just eyeball the code, and fix what they think will help, and often but not always it does help, some.
That's guesswork.
To get real speedup, you need to find all the problems, not just the few you can guess.
If your program is doing new, then chances are at some point that will be what you need to fix.
But it's not the only thing.
Here's the theory behind it.
For high-performance trading engines at good HFT shops, avoiding new/malloc in C++ code is a basic.
My generic class takes a Func<T> parameter in constructor, and I want to test one of its methods which basically invoke the constructor parameter.
I use Moq and in my test code is something like this:
[Fact]
public void Construct_EnsureInvokeFunction()
{
_objectToTest=new TypeToTest<T>(It.IsAny<Func<T>>());
_objectToTest.Construct();
//here I want to ensure that the passed parameter is invoked
//however since I can't mock Func<T>, I dont know how to verify
//whether Invoke method of Func<T> is triggered
}
One workaround that I can think is to wrap my Func<T> inside a new interface and create a method to wrap it's Invoke method, and then use Moq to mock the interface. However it doesn't seem effective.
Am I missing something? Any idea will be appreciated.
Thanks,
Anton
You can create a simple fake Func<T> closure with side-effects and verify those side-effects. A good one is incrementing a local variable because it also lets you assert that it was not called more than it should. Something like this:
int counter = 0;
Func<T> f = () => { counter++; return fakeT; }
var objectToTest = new TypeToTest<T>(f);
objectToTest.Construct();
Assert.Equal(1, counter);
You can wrap the Func<T> in an anonymous method that sets a local variable:
bool wasInvoked = false;
Func<T> testFunc = () => { var ret = funcToTest(); wasInvoked = true; return ret; }
// Test something with testFunc instead of funcToTest
...
Assert.IsTrue(wasInvoked);