首页 \ 问答 \ 在另一个泛型方法中是否允许调用泛型方法?(Is the invocation of a generic method allowed inside another generic method?)

在另一个泛型方法中是否允许调用泛型方法?(Is the invocation of a generic method allowed inside another generic method?)

我正在与Unity进行RTS游戏。 我的游戏中有很多类型的资源,例如树,农场。 每个资源都是一个GameObject,并拥有自己的主脚本来控制它。

防爆。 我想收获一棵树,我称之为。

gameObject.GetComponent<Tree>().Harvest();

如果我想收获农场,我会调用相同的脚本,但将“树”更改为“农场”,这很好,但是代码会被复制。 所以我使用这种泛型方法来抽象它。

void Harvest<T>(){
    gameObject.GetComponent<T>().Harvest();
}

但是C#编译器不会让我这样做。 我想知道是否可以定义使用泛型方法的泛型方法? 如果没有,有没有什么办法像这样抽象我的代码? 谢谢。

错误信息:

'T'不包含'Harvest'的定义,并且没有扩展方法'Harvest'接受类型'T'的第一个参数可以被找到(你是否缺少using指令或者程序集引用?)[Assembly-CSharp]


I'm making a RTS game with Unity. There're many types of resources in my game, such as, tree, farm. Each resource is a GameObject and has it own main script controlling it.

Ex. I want to harvest a tree, I call this.

gameObject.GetComponent<Tree>().Harvest();

If I want to harvest farm I call the same script but change "Tree" to "Farm" which is fine but code will be duplicated. So I abstract it by using generics method like this.

void Harvest<T>(){
    gameObject.GetComponent<T>().Harvest();
}

But the C# compiler won't let me do that. I want to know is it possible to define generics method that use generics method inside? If not, Is there any way to abstract my code like this? Thank you.

Error message:

'T' does not contain a definition for 'Harvest' and no extension method 'Harvest' accepting a first argument of type 'T' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]


原文:https://stackoverflow.com/questions/48035922
更新时间:2024-02-14 12:02

最满意答案

您可以通过查看前几个字节来确定它可能是这些格式之一。 然后,您应该测试以确定它是否真的其中之一,使用相关实用程序对该格式的完整性检查,或实际进行解压缩。

您可以在说明中找到标题格式:

其他:

  • zlib(.zz)格式描述 ,以两个字节(以位为单位)开始0aaa1000 bbbccccc,其中选择ccccc以使第一个字节被看作int16乘以256加上第二个字​​节被看作int16是31的倍数。例如: 01111000(比特)= 120(int16),10011100(比特)= 156(int16),120 * 256 + 156 = 30876,它是31
  • 压缩(.Z)以0x1f,0x9d开头
  • bzip2(.bz2)以0x42,0x5a,0x68开头

You can determine that it is likely to be one of those formats by looking at the first few bytes. You should then test to see if it really is one of those, using an integrity check from the associated utility for that format, or by actually proceeding to decompress.

You can find the header formats in the descriptions:

Others:

  • zlib (.zz) format description, starts with two bytes (in bits) 0aaa1000 bbbccccc, where ccccc is chosen so that the first byte viewed as a int16 times 256 plus the second byte viewed as a int16 is a multiple of 31. e.g: 01111000(bits) = 120(int16), 10011100(bits) = 156(int16), 120 * 256 + 156 = 30876 which is a multiple of 31
  • compress (.Z) starts with 0x1f, 0x9d
  • bzip2 (.bz2) starts with 0x42, 0x5a, 0x68

相关问答

更多
  • 您可以通过查看前几个字节来确定它可能是这些格式之一。 然后,您应该测试以确定它是否真的是其中之一,使用相关实用程序对该格式的完整性检查,或实际进行解压缩。 您可以在说明中找到标题格式: Zip(.zip)格式描述 ,从0x50,0x4b,0x03,0x04开始(除非空 - 然后最后两个是0x05,0x06或0x06,0x06) Gzip(.gz)格式描述 ,从0x1f,0x8b,0x08开始 xz(.xz)格式说明 ,以0xfd,0x37,0x7a,0x58,0x5a,0x00开头 其他: zlib(.zz ...
  • 红宝石filemagic宝石将做到这一点: require 'filemagic' puts FileMagic.new(FileMagic::MAGIC_MIME).file(__FILE__) # => text/x-ruby; charset=us-ascii 这个宝石根本没有看文件扩展名。 它读取一些文件内容并使用它来猜测文件的类型。 The ruby-filemagic gem will do it: require 'filemagic' puts FileMagic.new(FileMa ...
  • 鉴于你对扩展的看法,我可以看到几种方法 第一:你可以使用扩展名来确定文件的类型吗? 许多文件类型都有标准标题,所以你可以解析标题并确定这是你实现过滤器的常用文件类型的十几个之一。 第二:一个简单的hurestic会从文件的中间抓取100个字节,看看这是不是标准的ascii,例如每个字节的值都在9到126之间。这在给定的时间百分比内是错误的,不起作用很多语言的文本,并且不能用于unicode文本。 Given what you say about extensions I can see a couple o ...
  • 我想要做的是将压缩设置为“无”。 如果输入makeappx pack /? 你会看到有一个/nc选项,它会阻止该工具压缩文件: 所以命令将是makeappx pack /v /nc 。 What I'd like is to do is set the Compression to "None". If you type makeappx pack /? you will see there is a /nc option, which will prevent the tool from compress ...
  • 我猜你已经读过文件大小限制了: powershell compress-archive文件大小问题 PowerShell中的zip文件大小 压缩-归档 由于Compress-Archive依赖于Microsoft .NET Framework API System.IO.Compression.ZipArchive来压缩文件,因此使用Compress-Archive可以压缩的最大文件大小目前为2 GB。 这是底层API的限制。 也许你可以使用7zip的音量选项/v4GB I guess you've alr ...
  • JPEG图像文件和MP3音频文件已经过压缩。 除了以未压缩格式存储的一些元数据(例如JPEG图像上的EXIF标记)之外,压缩算法在这些类型的文件上几乎没有取得任何进展。 请记住,压缩算法并不神奇 - 通过大量推广,它们通过查找文件中的模式并以更有效的方式编码这些模式来工作。 如果文件已经压缩,它将不包含压缩算法可以使用的任何模式,因此它将无法生成较小的输出文件。 JPEG image files and MP3 audio files are already compressed. Aside from s ...
  • 将您的最后一行更改为: myBitmap.Save(new_fileName, myImageCodecInfo, myEncoderParameters); Change your last line to: myBitmap.Save(new_fileName, myImageCodecInfo, myEncoderParameters);
  • XFS没有压缩这样的功能。 另见Wikipedia在功能框中提到它 。 你必须提供更高级别 - 或者可能是更低级别,如果存在(不是我知道,但是一个人永远不知道) XFS has no such facility as compression. See also Wikipedia which mentions it in the features box. You would have to provide it on a higher level - or maybe at a lower one, if ...
  • 由于file输入将在没有选择file情况下启动,并且没有用户交互就无法选择文件,我建议使用change()方法(使用change事件): $('input:file').change( function(e){ console.log('file "' + path.split('\\').pop(); + '" selected.'); }); 参考文献: :file选择器 。 使用jQuery获取文件输入的选定文件名而不使用路径 As a file input will ...
  • 您将几个单独的参数传递给open()函数: with open(first_answer, ".", second_answer, 'rb') as in_file: open()函数的第三个位置参数是buffer参数,如果指定则始终必须是整数。 您需要使用+连接这些字符串或使用字符串格式来使其成为一个参数: with open(first_answer + "." + second_answer, 'rb') as in_file: You are passing in several separat ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。