简体   繁体   English

在C#中将“此”类类型作为参数传递

[英]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. 因为它将特定于类。

You can probably just pass a Type : 您可能只需传递一个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 这是一个演示: http : //ideone.com/oYQh5X

This method of clearing a static int is slow. 这种清除静态int的方法很慢。 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. 我的测试运行了该过程50,000次,因此本质上都不是“慢”的,但是相对而言,您的方法大约是。 700X slower then the traditional method. 比传统方法慢700倍。

    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. 在测试中,从HasStaticObjects类中删除3个字符串变量,即使没有字符串,您的方法仍将花费一些时间来查找字符串。

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() . 现在,让所有对象派生该接口,然后可以调用.ClearStaticValues()

This allows you to tailor your ClearStaticValues method for each class. 这使您可以为每个类定制ClearStaticValues方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM