在基类的lazy方法中获取继承者类名

时间:2016-12-02 09:45:14

标签: c# oop generics static lazy-initialization

这里是C#小提琴的链接 https://dotnetfiddle.net/4QTGx3

我试图在基类的静态方法中检索继承者类名。我找不到任何帮助。

public class Worker
{
    public string Id1 {get; set;}
    public string Id2 {get; set;}
    public string Id3 {get; set;}
}

public interface IModel
{
}

public class Model1: IModel
{
}

public class Model2: IModel
{
}

public class ModelActor <TModel> where TModel: IModel
{
    private Lazy<Worker> _worker = new Lazy<Worker>(
        () => new Worker() 
        {
            Id1 = new StackFrame().GetMethod().DeclaringType.Name,
            Id2 = MethodBase.GetCurrentMethod().DeclaringType.Name,
            Id3 = typeof(ModelActor <TModel>).Name
        }
    );

    public Worker Worker
    {
        get
        {
            return _worker.Value;
        }
    }
}

public class ModelActor1: ModelActor<Model1>
{
}

public class ModelActor2: ModelActor<Model2>
{
}

public static void Main()
{
    Console.WriteLine("Id1: {0} {1}", new ModelActor1().Worker.Id1,  new ModelActor2().Worker.Id1);
    Console.WriteLine("Id2: {0} {1}", new ModelActor1().Worker.Id2,  new ModelActor2().Worker.Id2);
    Console.WriteLine("Id3: {0} {1}", new ModelActor1().Worker.Id3,  new ModelActor2().Worker.Id3);

    Console.WriteLine("Want to get: ModelActor1 ModelActor2");
}

1 个答案:

答案 0 :(得分:0)

好的修好了。实际上,您需要添加第二个ModelActor类,因为只有一个签名被调用。

using System;
using System.Reflection;
using System.Diagnostics;

public class Program
{

public class Worker
{
    public string Id1 {get; set;}
    public string Id2 {get; set;}
    public string Id3 {get; set;}

    public string ModelName {get; set;}
}

public interface IModel
{
}

public class Model1: IModel
{
}

public class Model2: IModel
{
}

public class ModelActor <TModel> where TModel: IModel
{
    private Lazy<Worker> _worker = new Lazy<Worker>(
        () => {
                Console.WriteLine("Worker Instance created");

                return new Worker() 
                {
                    Id1 = new StackFrame().GetMethod().DeclaringType.Name,
                    Id2 = MethodBase.GetCurrentMethod().DeclaringType.Name,
                    Id3 = typeof(ModelActor <TModel>).Name,

                    ModelName = typeof(TModel).Name
                };
        }
    );

    public Worker Worker
    {
        get
        {
            return _worker.Value;
        }
    }
}


public class ModelActor2 <TModel> where TModel: IModel
{
    private Lazy<Worker> _worker = new Lazy<Worker>(
        () => {
                Console.WriteLine("Worker Instance created");

                return new Worker() 
                {
                    Id1 = new StackFrame().GetMethod().DeclaringType.Name,
                    Id2 = MethodBase.GetCurrentMethod().DeclaringType.Name,
                    Id3 = typeof(ModelActor <TModel>).Name,

                    ModelName = typeof(TModel).Name
                };
        }
    );

    public Worker Worker
    {
        get
        {
            return _worker.Value;
        }
    }
}

public class ModelActor1: ModelActor<Model1>
{
}

public class ModelActor2: ModelActor2<Model2>
{
}

public static void Main()
{
    Console.WriteLine("Id1: {0} {1}", new ModelActor1().Worker.Id1,  new ModelActor2().Worker.Id1);
    Console.WriteLine("Id2: {0} {1}", new ModelActor1().Worker.Id2,  new ModelActor2().Worker.Id2);
    Console.WriteLine("Id3: {0} {1}", new ModelActor1().Worker.Id3,  new ModelActor2().Worker.Id3);

    Console.WriteLine("Want to get: ModelActor1 ModelActor2\n");

    Console.WriteLine("ModelName: {0} {1}", new ModelActor1().Worker.ModelName,  new ModelActor2().Worker.ModelName);
    Console.WriteLine("No problem with model name.");
}
}