使用通用类型'System.Action'需要1个类型参数 [英] Using the generic type 'System.Action'requirs 1 type arguments

查看:385
本文介绍了使用通用类型'System.Action'需要1个类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI所有codeproject成员,
什么意思?

HI ALL codeproject members,
What does it mean?

Using the generic type ‘System.Action''<t>requires 1 type arguments</t>


谢谢

推荐答案

首先,有很多信息:一个简单的google可以使您很快地获得信息:MSDN [ ^ ]

您使用Action< T>委托将一个方法作为参数传递给另一个方法,而无需声明正式的委托.您传递的方法不能返回任何值,并且采用类型为"T"的单个参数.

这是直接从MSDN直接获取的:
First off, there is a lot of info out there: a simple google could have got you relevenat info very quickly: MSDN[^]

You use an Action<T> delegate to pass a method as a parameter to another method without having to declare a formal delegate. The method you pass can return no value, and takes a single parameter of type "T".

This is taken direct from MSDN:
delegate void DisplayMessage(string message);
   public static void Main()
   {
      DisplayMessage messageTarget; 
      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = ShowWindowsMessage;
      else
         messageTarget = Console.WriteLine;
      messageTarget("Hello, World!");   
   }      
   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }


可以简化为:


Can be simplified to:

   public static void Main()
   {
      Action<string> messageTarget; 
      if (Environment.GetCommandLineArgs().Length > 1)
         messageTarget = ShowWindowsMessage;
      else
         messageTarget = Console.WriteLine;
      messageTarget("Hello, World!");   
   }      
   private static void ShowWindowsMessage(string message)
   {
      MessageBox.Show(message);      
   }
</string>

这两个示例都执行相同的工作:您可以指定将输出预先发送到消息框(通过ShowWindowsMessage)或发送到控制台,而不必再次检查.
第二个示例只是省去了声明一个委托来定义可以使用的方法签名的麻烦.

Both examples do the same job: you can specify that output goes to a message box (via ShowWindowsMessage) or to the console once in advance, and never have to check again.
The second example just saves you having to declare a delegate to define what method signatures can be used.


编译器错误准确地告诉了您这一点.您正在使用System.Action< t>需要一个类型参数,而您没有传递它.请参考以下链接:

http://msdn.microsoft.com/en-us/library/018hxwa8.aspx [ ^ ]
The compiler error tells you that exactly. You are using System.Action<t> that expects a type argument and you are not passing that. Refer the link below:

http://msdn.microsoft.com/en-us/library/018hxwa8.aspx[^]


我认为您首先需要了解泛型.这真的很重要.了解它之后,您的问题对您来说就不会成为问题.

从这里开始:
http://msdn.microsoft.com/en-us/library/ms172192.aspx [ ^ ].

—SA
I think you need to learn about generics first. This is really important. After you understand it, your problem won''t be a problem for you.

Start here:
http://msdn.microsoft.com/en-us/library/ms172192.aspx[^].

—SA


这篇关于使用通用类型'System.Action'需要1个类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆