gpt4 book ai didi

c# - 在基类的惰性方法中获取继承类名

转载 作者:行者123 更新时间:2023-11-30 18:18:40 26 4
gpt4 key购买 nike

这里是 C# fiddle 的链接 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");
}

最佳答案

好的,修好了。实际上,您需要添加第二个 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.");
}
}

关于c# - 在基类的惰性方法中获取继承类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40929074/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com