【发布时间】:2014-07-10 19:47:25
【问题描述】:
我有一些代码使用匿名类型的反射将数据填充到 URI 模式中,如下所示:
var uri = UriUtility.FromPattern("/{action}/{id}.{contentType}", new {
action = "foo",
id = 1234,
contentType = "xml"
});
当我使用 MonoTouch 选项“链接所有程序集”时,匿名类型成员被剥离,我的代码在运行时失败。有没有办法告诉 MonoTouch 链接器保留所有匿名类型的成员?
更新
这里有一个完整的测试用例来演示这个问题。仅链接 SDK 程序集时测试通过,链接所有程序集时失败。
using System;
using System.Collections.Generic;
using System.Reflection;
using MonoTouch.Foundation;
using NUnit.Framework;
namespace LinkerTests.iOS
{
[Preserve(AllMembers = true)]
[TestFixture]
public class AnonymousTypeTests
{
[Test]
public void Test()
{
var str = string.Join(",", AnonymousTypeUtil.GetProperties(new { a = "a", b = "b" }));
Assert.AreEqual("a=a,b=b", str);
}
}
internal static class AnonymousTypeUtil
{
public static IEnumerable<string> GetProperties(object o)
{
foreach (var property in o.GetType().GetRuntimeProperties())
{
object value = property.GetValue(o);
yield return property.Name + "=" + value;
}
}
}
}
【问题讨论】:
-
听起来您的问题与匿名类型实际上是内部的而不是公共的这一事实有关,因此您将无法从其他程序集中引用该类型。我还想说明一个事实,我强烈建议您不要使用这种特殊的编码模式。您不应该将成员的姓名视为数据;改为使用成对的字符串。
-
我不认为内部与公共有任何关系。链接器删除公共成员和类型以及内部。
标签: c# xamarin.ios xamarin