如何解决MonoTouch JIT似乎不喜欢自定义哈希集类
| 我正在努力使FarseerPhysics在MonoTouch中进行编译。当我在System.Collections.Generic中使用HashSet时,它可以很好地工作,但是Farseer有它自己的用于Xbox 360和Windows Phone的Hashset类,因此我认为也为IPHONE包括该哈希集是有意义的。 这是Farseer哈希集代码:#if WINDOWS_PHONE || XBOX || IPHONE
//TODO: FIX
using System;
using System.Collections;
using System.Collections.Generic;
namespace FarseerPhysics.Common
{
public class HashSet<T> : ICollection<T>
{
private Dictionary<T,short> _dict;
public HashSet(int capacity)
{
_dict = new Dictionary<T,short>(capacity);
}
public HashSet()
{
_dict = new Dictionary<T,short>();
}
// Methods
#region ICollection<T> Members
public void Add(T item)
{
// We don\'t care for the value in dictionary,Keys matter.
_dict.Add(item,0);
}
public void Clear()
{
_dict.Clear();
}
public bool Contains(T item)
{
return _dict.ContainsKey(item);
}
public void CopyTo(T[] array,int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(T item)
{
return _dict.Remove(item);
}
public IEnumerator<T> GetEnumerator()
{
return _dict.Keys.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _dict.Keys.GetEnumerator();
}
// Properties
public int Count
{
get { return _dict.Keys.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
#endregion
}
}
#endif
它们的用法如下:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using FarseerPhysics.Collision;
using FarseerPhysics.Common;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Dynamics.Joints;
using Microsoft.Xna.Framework;
class World
{
(...)
private HashSet<Body> _bodyAddList = new HashSet<Body>();
private HashSet<Body> _bodyRemoveList = new HashSet<Body>();
private HashSet<Joint> _jointAddList = new HashSet<Joint>();
private HashSet<Joint> _jointRemoveList = new HashSet<Joint>();
}
当我将IPHONE添加到Farseer哈希集类文件中的#if时,有两个问题。
首先是我在声明中出现错误,编译器说HashSet是System.Collections.Generic.HashSet和FarseerPhysics.Common.HashSet之间的模糊引用。在Visual Studios编译器中不会发生此错误。我怀疑这是因为MonoTouch确实实现了Hashset,而Xbox 360和Windows Phone .Net API都没有。不太确定为什么这两个都没有哈希集,但我怀疑最好使用Farseers的哈希集。
另一个问题是,如果我在iPhone设备上运行应用程序时显式设置了声明以使用FarseerPhysics.Common.Hashset(即新的FarseerPhysics.Common.HashSet();),则会收到错误消息
\'尝试JIT编译方法\'System.Collections.Generic.Dictionary \'2:.ctor()\'
仅使用--aot运行时。\\ n \'
我还应该指出,该错误不会在模拟器中发生,而只会在实际设备上发生。
解决方法
由于含糊不清的引用,第一个问题是因为现在您有两个正在被类使用的名为HashSet的类,并且您没有指定要使用的类。您可以删除
using System.Collections.Generic;
行,或在文件顶部添加using HashSet = FarseerPhysics.Common.HashSet;
语句。这将使编译器知道专门使用哪个编译器。
您遇到的JIT编译错误是monotouch的一些限制之一:您不能真正在字典键中使用值类型,因为mono编译器将尝试实例化比较器对象。有关更多信息,请参见此处:http://monotouch.net/Documentation/Limitations(搜索“值类型作为字典键”)。
要变通解决此问题,您需要以新的类型实现IEqualityComparer接口,并将该类型的实例提供给Dictionary(IEqualityComparer)构造函数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。