AKD
AKD

Reputation: 3966

pass "this" class type as a parameter in c#

I want to create a common method for all classes, --->

RestMethods.ClearAllStaticValues(this);

so here I am passing this as a argument, which is a class reference. But how can i catch this in my method definition, in which I am processing that class fields (using reflection), currently I am doing that in my same class. The code below--->

var varList = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Static).ToList();
varList.Where(x => x.FieldType == typeof(Int32)).ToList().ForEach(x => x.SetValue(this, 0)); 

note: I don't wanna use it like this--->

Class A
{
     RestMethods.ClearAllStaticValues(this);
}

& method definition--->

RestMethods.ClearAllStaticValues(A args);

because it will class specific.

Upvotes: 0

Views: 187

Answers (3)

Jastill
Jastill

Reputation: 656

Instead of using reflection, use an interface:

interface ClearValues
{
    void ClearStaticValues();
}
class AnyClass : ClearValues
{
    private static int A, B, C;
    public void ClearStaticValues()
    {
        A = 0;
        B = 0;
        C = 0;
    }
}

Now have all your objects derive that interface and then you can call .ClearStaticValues().

This allows you to tailor your ClearStaticValues method for each class.

Upvotes: 0

Jastill
Jastill

Reputation: 656

This method of clearing a static int is slow. It is a creative way to remove all integer values from a class, but by the time you introduce all the other object types you wish to clear, this will get even slower.

My test runs this process 50,000 times, so neither are "slow" in essence, but comparatively your method is approx. 700X slower then the traditional method.

    static void Main(string[] args)
    {
        Stopwatch sw = Stopwatch.StartNew();

        for (int i = 0; i < 50000; i++)
        {
            HasStaticObjects hso1 = new HasStaticObjects();
            ClearAllStaticValues(hso1);
        }
        Console.WriteLine("Clear Generic Static Values: \n" + sw.Elapsed);

        sw.Restart();
        for (int i = 0; i < 50000; i++)
        {
            HasStaticObjects hso2 = new HasStaticObjects();
            hso2.ClearStaticValues();
        }
        Console.WriteLine("Clear Static Values: \n" + sw.Elapsed);
        Console.ReadLine();
    }

    public static void ClearAllStaticValues<T>(T currentClass)
    {
        var varList = currentClass.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Static).ToList();
        varList.Where(x => x.FieldType == typeof(Int32)).ToList().ForEach(x => x.SetValue(null, 0));
        varList.Where(x => x.FieldType == typeof(string)).ToList().ForEach(x => x.SetValue(null, ""));
    }
}

class HasStaticObjects
{
    private static int A, B, C;
    private static string D, E, F;
    public HasStaticObjects()
    {
        A = 1;
        B = 2;
        C = 3;
        D = "Hi";
        E = "Good";
        F = "Fast";
    }
    public void ClearStaticValues()
    {
        A = 0;
        B = 0;
        C = 0;
        D = "";
        E = "";
        F = "";
    }

Other cases that you need to accept will be times such as when an object contains no integers, or no strings and how will your method's performance be affected by that??

In the test remove the 3 string variables from the HasStaticObjects class and you will see that your method will still take time to find strings even though none exist.

Upvotes: 0

pascalhein
pascalhein

Reputation: 5846

You can probably just pass a Type:

public static void ClearAllStaticValues(Type t)
{
    var varList = t.GetFields(BindingFlags.NonPublic | BindingFlags.Static);
    varList.Where(x => x.FieldType == typeof(Int32)).ToList().ForEach(x => x.SetValue(null, 0)); 
}

Call it like this:

public class A
{
    public static void Clear()
    {
        //static member 
        RestMethods.ClearAllStaticValues(typeof(A));
    }
    public void ClearInstance()
    {
        //instance member
        RestMethods.ClearAllStaticValues(GetType());
    }
}

Here is a demo: http://ideone.com/oYQh5X

Upvotes: 4

Related Questions