![](/img/trans.png)
[英]How to access properties that are in an Inherited UserControl in C#?
[英]How to get access to inherited object properties in WCF?
案例是:
[ServiceContract]
public interface IInfo
{
[DataMember]
int Id{get;set;}
}
[DataContract]
[KnownType(typeof(Legal))]
public class Info
{
[DataMember]
public int Id { get; set; }
}
[DataContract]
public class Legal : Info
{
[DataMember]
public string ManagerName { get; set; }
}
[ServiceContract]
[ServiceKnownType(typeof(Legal))]
public interface IMyService
{
[OperationContract]
int DoWork(Info dto);
}
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService : IMyService
{
public int DoWork(Info dto)
{
string name;
if (dto is Legal)
name = (dto as Legal).ManagerName;
return dto.Id;
}
}
是否可以將 dto 知道為 Legal 類型並可以訪問子屬性?
我想存儲 dto,我不想為每個 info 子項提供很多服務。
將 Generics 傳遞給服務不起作用,wsdl 錯誤,IInfo 等接口作為輸入參數不起作用,轉換錯誤,基礎 class 像 Info 不起作用,可訪問的工作堆棧此不可用這是我第二次發布這個問題但沒有答案!
我將 json 作為 dto 傳遞給 MyService。 如果我添加“__type”:“Legal:#Dto”,MyService 將 dto 識別為 Legal。 然后 (dto as Legal).ManagerName 具有價值
該解決方案有效,實際上傳遞 __type 並不是很方便的方法。 我會感謝你更好的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.