【问题标题】:how to achieve pass TType value by the MyEvents function [closed]如何通过 MyEvents 函数实现传递 TType 值
【发布时间】:2014-06-04 09:47:58
【问题描述】:

在这段代码中,MyEvents 出现了一个错误。那里发生了什么。以及如何通过 MyEvents 函数实现传递 TType 值

public class MListBox : ListBox
{
    public Type TType { get; set; }

     public void LoadList()
    {
        MyEvents<TType , TType >.LoadList("getList").ForEach(w => this.Items.Add(w));
    }
}

public void main(string[] args)
{
  MListBox mb= new MListBox();
  mb.TType = typeof(STOCK);
  mb.LoadList();
}


 public static class MyEvents<T,M> where T : class where M : class
{
    public static M m;
    public static T t;
    public static List<objectCollection> LoadList(string _method)
    {
        m = Activator.CreateInstance<M>();
        MethodInfo method = typeof(M).GetMethod(_method);
        List<objectCollection> ret = (List<objectCollection>)method.Invoke(m, null);
        return ret;
    }
}

public class STOCK()
{
}

谢谢,

【问题讨论】:

  • 给出错误总是有帮助的。
  • 谢谢你,但是我如何在我的自定义列表框中传递 t-class
  • T 不是变量。
  • 我在代码中没有看到任何错误,你可以像这样使用它MListBox&lt;YourClass&gt; a = new MListBox&lt;YourClass&gt;(); 所以模板变量 T 在这种情况下将是 YourClass

标签: c# .net wpf c#-3.0


【解决方案1】:

您正在潜入泛型世界。使用泛型类型参数 T,您可以编写其他客户端代码可以使用的单个类,而不会产生运行时强制转换或装箱操作的成本或风险。

泛型类,例如MListBox&lt;T&gt; 不能按原样使用,因为它不是真正的类型。所以要使用MListBox&lt;T&gt;,您必须通过在尖括号内指定类型参数来声明和实例化构造类型。

例如

MListBox<YourClass> a = new MListBox<YourClass>();

YourClass 是你打算传入的类型参数

更多关于泛型的信息

http://msdn.microsoft.com/en-us/library/512aeb7t.aspx

更新

    public class MListBox<T, M> : ListBox
        where T : class
        where M : class, new()
    {
        public void LoadList()
        {
            MyEvents<T, M>.LoadList("getList").ForEach(w => this.Items.Add(w));
        }
    }

    public void main(string[] args)
    {
        MListBox<object,STOCK> mb = new MListBox<object,STOCK>();
        mb.LoadList();
    }


    public static class MyEvents<T, M>
        where T : class
        where M : class, new()
    {
        public static M m;
        public static T t;
        public static List<T> LoadList(string _method)
        {
            m = new M();
            MethodInfo method = typeof(M).GetMethod(_method);
            List<T> ret = (List<T>)method.Invoke(m, null);
            return ret;
        }
    }

要在 xaml 中使用的包装类

class StockListBox : MListBox<object,STOCK>
{
}

在 xaml 中用作,其中 local 是您的命名空间

<local:StockListBox />

【讨论】:

  • 谢谢...我必须将此控件放入 xaml 中。但它显示为 并在 xaml 代码中出现错误...
  • 您可以创建一个包装器class StockListBox : MListBox&lt;object,STOCK&gt; 并在 xaml 中使用该类,因为 xaml 不支持泛型类声明,除非您使用的是 xaml 2009。如果您使用的是 xaml 2009,那么您可以像这样使用&lt;MListBox x:TypeArguments="x:object,l:STOCK"/&gt;
  • 我在我的 xaml 列表框中使用此代码,但 x:TypeArguements 出现错误。我正在使用 2010 视觉工作室
  • 通常 xaml 2009 可用于框架 4,但仅适用于松散的 xaml,因此如果您使用 vs2010 构建桌面应用程序并混合,那么您可能不会使用 2009。如果您有单独的 xaml 文件并通过加载它们xamlrader 它将工作。所以我建议你使用包装类方法,因为它也会在 vs2010 中编译。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-16
  • 2018-04-02
  • 2019-05-22
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多