The easiest way to reuse a function without instantiating a new class

I currently have a function that looks like this:

public void AnimateLayoutTransform(object ControlToAnimate)
{
//Does some stuff
}

I use this feature in many different projects, so I want it to be very reusable. So now I have it in a .cs file enclosed in a namespace and class:

namespace LayoutTransformAnimation
{
    public class LayoutAnims
    {
        public void AnimateLayoutTransform(object ControlToAnimate)
        {
            //Do stuff
        }
    }
}

The problem is that to use this single function in this project I need to do something like

new LayoutTransformAnimation.LayoutAnims().AnimateLayoutTransform(mygrid);

Which just seems like a lot of work to reuse one function. Is there a way to at least use a function without creating a new instance of the class? Just as we can Double.Parse()not create a new one double?

+3
4

- . - # 3.0 - :

public static class AnimationExtensions
{
    public static void AnimateLayoutTransform(this object controlToAnimate)
    {
        // Code
    }
}

:

mygrid.AnimateLayoutTransform();

, ""? ... , UIElement? , ... , .

+10
+5

util , .

public static class YourUtilsClass
{

    public static Void YourMethod()
    {
        //do your stuff
    }   

}

: YourUtilsClass.YourMethod()

+3
namespace LayoutTransformAnimation 
{ 
    public class LayoutAnims 
    { 
        public static void AnimateLayoutTransform(object ControlToAnimate) 
    { 
        //Do stuff 
    } 
} 

}

LayoutTransformAnimation.LayoutAnims.AnimateLayoutTransform(something);
+2

Source: https://habr.com/ru/post/1751777/


All Articles