C# Type.GetConstructor在Iphone上返回null
我正在用Xamarin为Iphone开发一个应用程序,类似应用程序的android版本已经准备好供Google play使用。我正在我的应用程序中使用jabber网络库来实现聊天功能。但是在设备上有一些问题(iPhone5-iOS7.0.3)。这个问题在emulator中不会发生,下面是方法代码C# Type.GetConstructor在Iphone上返回null,c#,ios,xamarin.ios,xamarin.android,xamarin,C#,Ios,Xamarin.ios,Xamarin.android,Xamarin,我正在用Xamarin为Iphone开发一个应用程序,类似应用程序的android版本已经准备好供Google play使用。我正在我的应用程序中使用jabber网络库来实现聊天功能。但是在设备上有一些问题(iPhone5-iOS7.0.3)。这个问题在emulator中不会发生,下面是方法代码 public class QnameType { /// <summary> /// Element name /// </summary> pr
public class QnameType
{
/// <summary>
/// Element name
/// </summary>
protected internal string Name;
/// <summary>
/// Element namespace URI
/// </summary>
protected internal string NS;
/// <summary>
/// Type to create for NS/Name pair
/// </summary>
protected internal Type ElementType;
/// <summary>
/// Create a QnameType
/// </summary>
/// <param name="name"></param>
/// <param name="ns"></param>
/// <param name="typ"></param>
public QnameType(string name, string ns, Type typ)
{
this.Name = name;
this.NS = ns;
this.ElementType = typ;
}
/// <summary>
/// Is this the same qname by element name and namespace?
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (obj == (object)this)
return true;
QnameType other = obj as QnameType;
if (other == null)
return false;
return (other.Name == Name) && (other.NS == NS);
}
/// <summary>
/// Get a hash over the name and namespace.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return ToString().GetHashCode();
}
/// <summary>
/// Namespace|Name
/// </summary>
/// <returns></returns>
public override string ToString()
{
return NS + "|" + Name;
}
}
public interface IPacketTypes
{
/// <summary>
/// QName to type mappings.
/// </summary>
QnameType[] Types { get; }
}
public class ElementFactory
{
private Hashtable m_types = new Hashtable();
private static readonly Type[] s_constructorTypes =
new Type[] { typeof(string),
typeof(XmlQualifiedName),
typeof(XmlDocument) };
public void AddType(IPacketTypes list)
{
foreach (QnameType qn in list.Types)
{
this.AddType(qn.Name, qn.NS, qn.ElementType);
}
}
public void AddType(string localName, string ns, Type t)
{
Debug.Assert(t.IsSubclassOf(typeof(Element)));
ConstructorInfo ci = t.GetConstructor(s_constructorTypes);
Debug.Assert(ci != null);
AddType(new XmlQualifiedName(localName, ns), ci);
}
public Element GetElement(string prefix, XmlQualifiedName qname, XmlDocument doc)
{
ConstructorInfo ci = (ConstructorInfo) m_types[qname];
if (ci == null)
{
return new Element(prefix, qname, doc);
}
return (Element) ci.Invoke
(new object[] {prefix, qname, doc});
}
/// <summary>
/// Get a constructor for the appropriate type for the given qname.
/// </summary>
public ConstructorInfo this[XmlQualifiedName qname]
{
get { return (ConstructorInfo) m_types[qname]; }
}
}
公共类QnameType
{
///
///元素名
///
受保护的内部字符串名称;
///
///元素名称空间URI
///
受保护的内部字符串NS;
///
///要为NS/名称对创建的类型
///
受保护的内部类型ElementType;
///
///创建一个QnameType
///
///
///
///
公共QnameType(字符串名称、字符串ns、类型类型)
{
this.Name=Name;
这是1.NS=NS;
this.ElementType=typ;
}
///
///元素名和名称空间是否相同?
///
///
///
公共覆盖布尔等于(对象对象对象)
{
如果(obj==(对象)此)
返回true;
QnameType other=obj作为QnameType;
如果(其他==null)
返回false;
返回(other.Name==Name)&&(other.NS==NS);
}
///
///获取名称和命名空间的哈希值。
///
///
公共覆盖int GetHashCode()
{
返回ToString().GetHashCode();
}
///
///名称空间|名称
///
///
公共重写字符串ToString()
{
返回NS+“|”+名称;
}
}
公共接口iPackeTypes
{
///
///QName到类型的映射。
///
QnameType[]类型{get;}
}
公共类元素工厂
{
私有哈希表m_types=新哈希表();
私有静态只读类型[]s_构造函数类型=
新类型[]{typeof(字符串),
类型(XmlQualifiedName),
typeof(XmlDocument)};
公共void AddType(iPackeTypes列表)
{
foreach(list.Types中的QnameType qn)
{
this.AddType(qn.Name、qn.NS、qn.ElementType);
}
}
public void AddType(字符串localName、字符串ns、类型t)
{
Assert(t.IsSubclassOf(typeof(Element));
ConstructorInfo ci=t.GetConstructor(s_constructorTypes);
Assert(ci!=null);
AddType(新的XmlQualifiedName(localName,ns),ci);
}
公共元素GetElement(字符串前缀,XmlQualifiedName qname,XmlDocument文档)
{
ConstructorInfo ci=(ConstructorInfo)m_类型[qname];
if(ci==null)
{
返回新元素(前缀、qname、单据);
}
返回(元素)ci.Invoke
(新对象[]{前缀,qname,doc});
}
///
///获取给定qname的适当类型的构造函数。
///
公共构造函数提供此[XmlQualifiedName qname]
{
获取{return(ConstructorInfo)m_类型[qname];}
}
}
t、 GetConstructor()在Iphone上返回null,但在模拟器上可以正常工作
编辑:添加更多细节
我们将非常感谢您的帮助
谢谢这可能很正常,取决于
t
本身-它代表什么类型
默认情况下,在模拟器构建上禁用托管链接器(不链接)。这意味着每种类型都将是应用程序的一部分
但是,设备构建的默认值为链接SDK。这意味着将从应用程序中删除未使用的类型(使用静态分析找到)。这允许减少应用程序的大小(不在每个应用程序中编译/发布整个.NET BCL)
静态分析无法发现代码的动态使用,例如反射。如果应用程序依赖于反射,则需要保留代码:使用[preserve]
属性、XML文件或添加一些额外的代码,这些代码将提示链接器保留所需的成员
有关更多详细信息,请参见。除了poupou的答案之外: 如果因为Unity3D存在类似问题而在这个问题上结结巴巴,请查看此页面: 通常,该页面向您展示了如何确保IL2CPP构建中包含所有内容:
- 对类使用
关键字[Preserve]
- 将
文件添加到资源根目录中link.xml
t
)。