我有一个关于这个序列化问题的奇怪案例 - 在本网站上已经多次询问过,我已经完成了一些问题并尝试了常用项目无济于事:
为了进一步解释,我在下面提供了我的代码的简化版本.本质上我使用一个WebServiceHost对象来运行RESTful服务,我的一个端点返回一个序列化为XML的对象(我用对象[DataContract]和[DataMember]属性注释了对象).此对象包含SerializableDictionary<string, object>(此处)值已键入的值object.我相信这就是它失败的原因:
显然,我无法用Objectcs注释[XmlInclude],因为它是一个服务,我不是自己序列化我不能使用类似的东西
new Serializer(typeof(...), new Type[] { ... }}
Run Code Online (Sandbox Code Playgroud)
我有什么想法可以做什么?我想过不要将dict值键入为对象,而是更具体但问题是这个值可以采用原语或cusotm类型.一些代码来解释上面的内容:
编辑:更新了以下代码,使其更加清晰
[DataContract]
public class ResponseObject
{
[DataMember(Name = "data")]
public SerializableDictionary<string, object> Data { get;set; }
public ResponseObject()
{
Data = new SerializableDictionary<string, object>();
}
}
...
var d1 = new ResponseObject();
d1.Data.Add("some key", "some value"); //WORKS AND SERIALIZES PERFECLTY
var d2 = new …Run Code Online (Sandbox Code Playgroud) 我有两个类,一个通过注册类型来设置容器,另一个包含我想要注入的静态属性.我的问题是属性永远不会通过注入设置,因此当我调用它的方法时,该属性始终为null.
public class ClassOne
{
public void Method()
{
Container.RegisterType<IClass, ClassImplOne>("ImplOne");
Container.RegisterType<IClass, ClassImplTwo>("ImplTwo");
}
}
public static class ClassTwo
{
[Dependency]
public static IClass SomeProperty { get; set; }
public static void SomeOtherMethod()
{
SomeProperty.AnotherMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我删除Dependency属性并在ClassOne中做一个简单的
ClassTwo.SomeProperty = Container.Resolve<IClass>("ImplOne");
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想知道是否可以这样做而不明确地为属性赋值(即容器可以通过属性注入)?
编辑:
谢谢.我已经从ClassTwo中删除了静态声明,并在ClassOne中添加了RegisterType和Resolve for ClassTwo,还添加了InjectionProperty:
Container.RegisterType<IClass, ClassImplOne>("ImplOne", new InjectionProperty("SomeProperty"));
Run Code Online (Sandbox Code Playgroud)
但它仍然不起作用:S
c# containers dependency-properties unity-container property-injection
我有一个无效的导入 - 该对象为null.最初它是一个ImportMany,但我把它简化为导入以尝试识别问题,但我没有成功这样做.
我已经浏览了这个网站和谷歌,并遵循主要想法:
我的代码设置如下(为了紧凑而简化):
Assembly1
public class MyBootstrapper
{
//Automatically called by ExcelDna library, I do not instantiate this class
public void AutoOpen()
{
var ac1 = new AssemblyCatalog(typeof(XLHandler).Assembly);
var ac2 = new AssemblyCatalog(typeof(MyComponent).Assembly);
var agc = new AggregateCatalog();
agc.Catalogs.Add(ac1);
agc.Catalogs.Add(ac2);
var cc = new CompositionContainer(agc);
try
{
cc.ComposeParts(this);
}
catch (CompositionException exception) {}
}
}
[Export]
public class XLHandler
{
[Import(typeof(IMyComponent))]
public IMyComponent _component;
public void SomeMethod()
{
//try to use _component but it …Run Code Online (Sandbox Code Playgroud) 我有一个接口的多种实现,并且我只想以编程方式导出一个。我已经看过了RegistrationBuilder,它的AddMetaData()功能是,但这是为导出定义MetaData,而不是为特定值过滤。例如,我想做这样的事情:
public enum MyClassType { TypeA, TypeB }
public interface IClass {}
public interface ClassMetaData { MyClassType Type { get; } }
[ExportMetadata("Type", MyClassType.TypeA)]
public MyClassA : IClass
{
public MyClassType Type { get { return MyClassType.TypeA; } }
}
[ExportMetadata("Type", MyClassType.TypeB)]
public MyClassB : IClass
{
public MyClassType Type { get { return MyClassType.TypeB; } }
}
//...Then in my bootstrapping class where I set up the MEF container...
var registrationBuilder = new RegistrationBuilder();
registrationBuilder.ForTypesDerivesFrom<IClass>().... …Run Code Online (Sandbox Code Playgroud) 我正在尝试访问我的iCloud日历curl(在我编写任何代码之前使用它作为测试),但是我收到了未经授权的访问错误.有没有人有幸让这个工作?我已经用它来提取我的用户ID,并通过给自己发送一个事件邀请来确定服务器ID.
命令运行
curl -u <apple email ID> https://<server id>-caldav.icloud.com/<user id>/calendars
Run Code Online (Sandbox Code Playgroud)
响应
<html><head><title>Unauthorized</title></head><body><h1>Unauthorized</h1><p>You are not authorized to access this resource.</p></body></html>
Run Code Online (Sandbox Code Playgroud)
编辑
我试图使用fiddler 复制此查询,但继续得到502响应 - 连接失败SecurityException.
c# ×4
mef ×2
caldav ×1
composition ×1
containers ×1
curl ×1
icloud ×1
icloud-api ×1
null ×1
rest ×1
xml ×1