在另一个泛型方法中是否允许调用泛型方法?(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
最满意答案
您可以通过查看前几个字节来确定它可能是这些格式之一。 然后,您应该测试以确定它是否真的是其中之一,使用相关实用程序对该格式的完整性检查,或实际进行解压缩。
您可以在说明中找到标题格式:
- 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)格式描述 ,以两个字节(以位为单位)开始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:
- Zip (.zip) format description, starts with 0x50, 0x4b, 0x03, 0x04 (unless empty — then the last two are 0x05, 0x06 or 0x06, 0x06)
- Gzip (.gz) format description, starts with 0x1f, 0x8b, 0x08
- xz (.xz) format description, starts with 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00
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
相关问答
更多-
如何检测文件上使用的压缩类型?(How to detect type of compression used on the file? (if no file extension is specified))[2022-09-16]
您可以通过查看前几个字节来确定它可能是这些格式之一。 然后,您应该测试以确定它是否真的是其中之一,使用相关实用程序对该格式的完整性检查,或实际进行解压缩。 您可以在说明中找到标题格式: 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 ...
-
有没有办法先前确定文件是否适合压缩?(Is there a way to previously determine if a file is a good candidate for compression?)[2023-01-23]
鉴于你对扩展的看法,我可以看到几种方法 第一:你可以使用扩展名来确定文件的类型吗? 许多文件类型都有标准标题,所以你可以解析标题并确定这是你实现过滤器的常用文件类型的十几个之一。 第二:一个简单的hurestic会从文件的中间抓取100个字节,看看这是不是标准的ascii,例如每个字节的值都在9到126之间。这在给定的时间百分比内是错误的,不起作用很多语言的文本,并且不能用于unicode文本。 Given what you say about extensions I can see a couple o ... -
将UWP应用程序打包为APPX时控制html文件的压缩(Control compression of html file when packaging UWP app as an APPX)[2023-07-29]
我想要做的是将压缩设置为“无”。 如果输入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 ...
-
Python文件压缩(Python File Compression)[2021-12-13]
JPEG图像文件和MP3音频文件已经过压缩。 除了以未压缩格式存储的一些元数据(例如JPEG图像上的EXIF标记)之外,压缩算法在这些类型的文件上几乎没有取得任何进展。 请记住,压缩算法并不神奇 - 通过大量推广,它们通过查找文件中的模式并以更有效的方式编码这些模式来工作。 如果文件已经压缩,它将不包含压缩算法可以使用的任何模式,因此它将无法生成较小的输出文件。 JPEG image files and MP3 audio files are already compressed. Aside from s ... -
使用C#进行Tiff文件压缩(Tiff file compression with C#)[2022-03-11]
将您的最后一行更改为: 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 ...
-
如何检测文件何时上传到文件类型的输入元素?(How to detect when a file is uploaded to an input element of type file?)[2021-12-16]
由于file输入将在没有选择file情况下启动,并且没有用户交互就无法选择文件,我建议使用change()方法(使用change事件): $('input:file').change( function(e){ console.log('file "' + path.split('\\').pop(); + '" selected.'); }); 参考文献: :file选择器 。 使用jQuery获取文件输入的选定文件名而不使用路径 As a file input will ... -
Python 3.5中的压缩,需要一个整数(得到类型str)(Compression in Python 3.5, an integer is required (got type str))[2022-11-09]
您将几个单独的参数传递给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 ...