gpt4 book ai didi

c# - 获取 C# 类中属性的确切类型

转载 作者:行者123 更新时间:2023-12-02 21:32:08 25 4
gpt4 key购买 nike

抱歉,标题很笼统,但很难用几句话解释我目前的问题是什么。

所以我有一个像这样的简单的类工厂:

    public Model Construct<T>(T param) where T : IModelable
{
new Model {Resource = param};
return n;
}

模型类如下所示:

public class Model
{
public object Resource { get; set; }
}

问题是,您可以看到,资源当前是一个对象。我希望资源应该是从构造中获取的类型,并且不会丢失类型安全......

我尝试使用类型参数解决它,但失败了,因为我可以使用类型参数扩展模型类,但如果我想将它存储到一个简单的类存储库怎么办?

然后 Construct 就可以工作,但是如果我想从存储库中获取实例化类,我必须再次声明类型参数,如下所示:

Repository.Get<Model<Spaceship>>(0) ....当然这是错误的,因为我希望模型本身知道在构造中添加了什么类型的资源。

有人知道如何处理这个问题吗?

整个代码目前如下所示:

/// <summary>
/// Class Repository
/// </summary>
public sealed class Repository
{
/// <summary>
/// The _lock
/// </summary>
private static readonly object _lock = new object();

/// <summary>
/// The _syncroot
/// </summary>
private static readonly object _syncroot = new object();

/// <summary>
/// The _instance
/// </summary>
private static volatile Repository _instance;

/// <summary>
/// The _dict
/// </summary>
private static readonly Dictionary<int, object> _dict
= new Dictionary<int, object>();


/// <summary>
/// Prevents a default data of the <see cref="Repository" /> class from being created.
/// </summary>
private Repository()
{
}

/// <summary>
/// Gets the items.
/// </summary>
/// <value>The items.</value>
public static Repository Data
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null) _instance = new Repository();
}
}
return _instance;
}
}

/// <summary>
/// Allocates the specified id.
/// </summary>
/// <param name="id">The id.</param>
/// <param name="parameter">The parameter.</param>
/// <resource name="id">The id.</resource>
/// <resource name="parameter">The parameter.</resource>
public void Allocate(int id, object parameter)
{
lock (_syncroot)
{
_dict.Add(id, parameter);
}
}

/// <summary>
/// Gets the specified id.
/// </summary>
/// <typeparam name="T">The type of the tref.</typeparam>
/// <param name="id">The id.</param>
/// <returns>``0.</returns>
/// <resource name="id">The id.</resource>
public T Get<T>(int id)
{
lock (_syncroot)
{
return (T) _dict[id];
}
}
}

/// <summary>
/// Class IModelFactory
/// </summary>
public sealed class ModelFactory
{
/// <summary>
/// The _lock
/// </summary>
private static readonly object _lock = new object();

/// <summary>
/// The _instance
/// </summary>
private static volatile ModelFactory _instance;

/// <summary>
/// Prevents a default instance of the <see cref="ModelFactory" /> class from being created.
/// </summary>
private ModelFactory()
{
}

/// <summary>
/// Gets the data.
/// </summary>
/// <value>The data.</value>
public static ModelFactory Data
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null) _instance = new ModelFactory();
}
}
return _instance;
}
}

/// <summary>
/// Constructs the specified param.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="param">The param.</param>
/// <returns>Model{``0}.</returns>
public Model Construct<T>(T param) where T : IModelable
{
var n = new Model {Resource = param};
return n;
}
}

/// <summary>
/// Class Model
/// </summary>
/// <typeparam name="T"></typeparam>
public class Model
{
public object Resource { get; set; }
}

/// <summary>
/// Interface IModelable
/// </summary>
public interface IModelable
{
/// <summary>
/// Gets or sets the mass.
/// </summary>
/// <value>The mass.</value>
float Mass { get; set; }
}

/// <summary>
/// Class spaceship
/// </summary>
public class Spaceship : IModelable
{
/// <summary>
/// Gets or sets the mass.
/// </summary>
/// <value>The mass.</value>
public float Mass { get; set; }
}

所以问题将在这里得到解决:

添加到存储库:

Repository.Data.Allocate(1, ModelFactory.Data.Construct(new Spaceship()));

没关系,但是之后:

var test_variable = Repository.Data.Get<Model>(1);

所以现在我有一个来自类型参数的非类型安全对象,我不知道 C 模型构造中存储了什么类型的类。

我非常感谢在 Model 类上使用类型参数的建议,但它会出现另一个问题,因为我必须用它更改 Get 函数:

var test_variable = Repository.Data.Get<Model<Spaceship>>(1);

但这绝对是错误的,因为我不知道模型中存储了什么样的类型的类...,所以我想在加载时避免这种类型参数定义来自存储库的实例。

最佳答案

您可以通过使您的 Model 类通用来解决此问题,如下所示:

public class Model<T> 
{
public T Resource { get; set; }
}

然后,您的 Construct 方法可以像这样工作:

public Model<T> Construct<T>(T param) where T : IModelable<T>
{
return new Model<T>() {Resource = param};
}

关于c# - 获取 C# 类中属性的确切类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22106172/

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