Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在一个字典中存储不同的类,允许执行_C#_Dictionary - Fatal编程技术网

C# 在一个字典中存储不同的类,允许执行

C# 在一个字典中存储不同的类,允许执行,c#,dictionary,C#,Dictionary,我有两门课: public class Variable<T>; public class Closure; 两者都有名为GetValue的方法: public T GetValue(); // Variable<T> public string GetValue(params string[] arguments); // Closure public string SetValue(object newValue); 这些类表示视频游戏、控制台组件属性 我想做

我有两门课:

public class Variable<T>;
public class Closure;
两者都有名为
GetValue
的方法:

public T GetValue(); // Variable<T>
public string GetValue(params string[] arguments); // Closure
public string SetValue(object newValue);

这些类表示视频游戏、控制台组件属性

我想做的是,将这两个类都放在一个
目录中
,同时允许轻松访问/操作类的公共属性和方法

我确实尝试添加了一个虚拟的
接口
,但它失去了与对象的关系,返回了接口实例,因此阻止了我使用这些公共属性、方法:

public static class Storage
{
    public static Dictionary<string, IConsoleProperty> Variables = new Dictionary<string, IConsoleProperty>();

    public static string Inform()
    {
        string output = "";

        foreach (var variable in Variables)
        {
            output += string.Format("{0} : {1}", variable.Key, variable.Value.description);
        }

        return output;
    }
}
公共静态类存储
{
公共静态字典变量=new Dictionary();
公共静态字符串通知()
{
字符串输出=”;
foreach(变量中的var变量)
{
output+=string.Format(“{0}:{1}”,variable.Key,variable.Value.description);
}
返回输出;
}
}
类型
Console.IConsoleProperty
不包含
description
的定义,并且找不到类型
Console.IConsoleProperty`的扩展方法(是否缺少using指令或程序集引用?)

我读到我应该在这样的场景中进行强制转换,但我不知道如何从字符串(
typeof(variable.Value)
)动态强制转换,特别是对于多类型的
Generic
实例

如何将这两个类都保存在一个目录中,但在检索值时,获取基类实例而不是接口?

首先,这些:

public string handle;
public string description;
不是公共属性,而是公共字段。公共财产是这样做的:

public string Handle { get; set; }
public string Description { get; set; }
不过,请考虑一下,你是否真的需要在课堂之外改变这些

不过,为了回答您的问题,您的两个类有一些共同的特点,但它们完全不同。因此,最干净的解决方案实际上是有两个字典。不要试图使两件事情完全相同,而事实并非如此

您可以通过调用对象上的
GetType()
方法来访问对象类型信息。您可以通过执行以下操作来检查它是否属于
T

if (myObj is T)
但没有办法把事情归结为“到底是什么”

首先,这些:

public string handle;
public string description;
不是公共属性,而是公共字段。公共财产是这样做的:

public string Handle { get; set; }
public string Description { get; set; }
不过,请考虑一下,你是否真的需要在课堂之外改变这些

不过,为了回答您的问题,您的两个类有一些共同的特点,但它们完全不同。因此,最干净的解决方案实际上是有两个字典。不要试图使两件事情完全相同,而事实并非如此

您可以通过调用对象上的
GetType()
方法来访问对象类型信息。您可以通过执行以下操作来检查它是否属于
T

if (myObj is T)

但没有办法把事情归结为“到底是什么”

您可能希望在
IConsoleProperty
界面中包含
handle
description
。这边
variable.Value
将返回一个
IConsoleProperty
,其中包含
句柄
说明
。然后您将能够使用
句柄
描述
。但是,如果要使用非共享公共成员,则必须强制转换

public interface IConsoleProperty 
{
    public string handle { get; set; }
    public string description { get; set; }
}

public class Variable<T> : IConsoleProperty
{
    public string handle { get; set; }
    public string description { get; set; }
    //Rest of Variable class
}
public class Closure : IConsoleProperty
{
    public string handle { get; set; }
    public string description { get; set; }
    //Rest of Closure class
}
公共接口IConsoleProperty
{
公共字符串句柄{get;set;}
公共字符串说明{get;set;}
}
公共类变量:IConsoleProperty
{
公共字符串句柄{get;set;}
公共字符串说明{get;set;}
//变量类的其余部分
}
公共类关闭:IConsoleProperty
{
公共字符串句柄{get;set;}
公共字符串说明{get;set;}
//关闭类的其余部分
}
如果您需要进行一些造型,您可以执行以下操作:

if (variable.Value is Closure)
{
    var myClosure = (Closure)variable.Value;
    //Do stuff with myClosure
}
//Susbstitute MyOtherClass with the appropriate type argument
if (variable.Value is Variable<MyOtherClass>) 
{
    var myVariable = (Variable<MyOtherClass>)variable.Value;
    //Do stuff with myVariable
}
if(variable.Value为闭包)
{
var myClosure=(Closure)variable.Value;
//用MyClose做点什么
}
//使用适当的类型参数替换MyOtherClass
if(variable.Value为variable)
{
var myVariable=(Variable)Variable.Value;
//用myVariable做一些事情
}

您可能希望在
IConsoleProperty
界面中包含
句柄
说明
。这边
variable.Value
将返回一个
IConsoleProperty
,其中包含
句柄
说明
。然后您将能够使用
句柄
描述
。但是,如果要使用非共享公共成员,则必须强制转换

public interface IConsoleProperty 
{
    public string handle { get; set; }
    public string description { get; set; }
}

public class Variable<T> : IConsoleProperty
{
    public string handle { get; set; }
    public string description { get; set; }
    //Rest of Variable class
}
public class Closure : IConsoleProperty
{
    public string handle { get; set; }
    public string description { get; set; }
    //Rest of Closure class
}
公共接口IConsoleProperty
{
公共字符串句柄{get;set;}
公共字符串说明{get;set;}
}
公共类变量:IConsoleProperty
{
公共字符串句柄{get;set;}
公共字符串说明{get;set;}
//变量类的其余部分
}
公共类关闭:IConsoleProperty
{
公共字符串句柄{get;set;}
公共字符串说明{get;set;}
//关闭类的其余部分
}
如果您需要进行一些造型,您可以执行以下操作:

if (variable.Value is Closure)
{
    var myClosure = (Closure)variable.Value;
    //Do stuff with myClosure
}
//Susbstitute MyOtherClass with the appropriate type argument
if (variable.Value is Variable<MyOtherClass>) 
{
    var myVariable = (Variable<MyOtherClass>)variable.Value;
    //Do stuff with myVariable
}
if(variable.Value为闭包)
{
var myClosure=(Closure)variable.Value;
//用MyClose做点什么
}
//使用适当的类型参数替换MyOtherClass
if(variable.Value为variable)
{
var myVariable=(Variable)Variable.Value;
//用myVariable做一些事情
}

IConsoleProperty看起来像什么?听起来它既没有
句柄
也没有
说明
。IConsoleProperty看起来像什么?听起来它既没有
句柄
也没有
说明
。+1。请注意,您可以使用
dynamic
…模拟“无论它是什么”。。。但是为不相关的对象创建单独的集合会更容易。如果对象相关,那么创建/实现公共接口是一个好方法。我可以选择双字典解决方案,但在这种情况下,