Static methods for common classes?

Ok, this is so:

I got a generic base class that I need to initialize with some static values. These values ​​have nothing to do with the types into which my base base class is loaded.

I want to do something like this:

GenericBaseclass.Initialize(AssociatedObject);

at the same time having a class that does the following:

public class DerivedClass : GenericBaseclass<int>
{
   ...
}

Is there any way to do this? I could not make a general template and put a static method there, but I don’t like this “hack” :)

+5
source share
2 answers

, . , .

, :

using System;

public class GenericType<TFirst, TSecond>
{
    // Never use a public mutable field normally, of course.
    public static string Foo;
}

public class Test
{    
    static void Main()
    {
        // Assign to different combination
        GenericType<string,int>.Foo = "string,int";
        GenericType<int,Guid>.Foo = "int,Guid";
        GenericType<int,int>.Foo = "int,int";
        GenericType<string,string>.Foo = "string,string";


        // Verify that they really are different variables
        Console.WriteLine(GenericType<string,int>.Foo);
        Console.WriteLine(GenericType<int,Guid>.Foo);
        Console.WriteLine(GenericType<int,int>.Foo);
        Console.WriteLine(GenericType<string,string>.Foo);

    }
}

, T , .

+14

, . , . .

, .

+6

All Articles