有能够实现C#定义的InterfaceB与InterfaceA之间的动态转换方法吗?

bobzhang 2011-03-24 02:08:25
现有两个接口定义

public interface A

{

int Fld1{get;set;};

int Fld2{get;set};

}

public interface B

{

int Fld1{get;set;};

int Fld2{get;set};

}

两个接口的定义一致,ClassB实现了InterfaceB.

使用场景:

分布式服务器甲,乙.

甲为代理服务器,乙为应用服务器.

在乙服务器上实例化了ClassB,并提供Remoting服务,以InterfaceB的形式提供给外部访问;

在甲服务器上开方InterfaceA给客户端使用,有能够实现InterfaceB与InterfaceA之间的动态转换方法吗?
...全文
204 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bobzhang 2011-03-27
  • 打赏
  • 举报
回复
To xingyuebuyu:
请问:(classb可以接口B来处理吗?
private void Form1_Load(object sender, EventArgs e)
{
classA ca = new classA();
ca.f1 = 1;
ca.f2 = 2;
A a1 = (A)ca;

MemoryStream ms = new MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(ms, a1);
ms.Position = 0;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf2 = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf2.Binder = new ubind(typeof(classb).FullName);
B b1 = (B)bf2.Deserialize(ms);

}
bobzhang 2011-03-25
  • 打赏
  • 举报
回复
谢谢各位的思路,小弟试试
xingyuebuyu 2011-03-24
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
classA ca = new classA();
ca.f1 = 1;
ca.f2 = 2;
A a1 = (A)ca;

MemoryStream ms = new MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf.Serialize(ms, a1);
ms.Position = 0;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf2 = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bf2.Binder = new ubind(typeof(classb).FullName);
B b1 = (B)bf2.Deserialize(ms);

}
}

public class ubind : SerializationBinder
{

private string strTypeName = "";
public ubind(string typeName)
{
strTypeName = typeName;
}
public override System.Type BindToType(string assemblyName, string typeName)
{
System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
typeName = strTypeName;
return ass.GetType(typeName);
}
}

public interface A
{
int f1 { get; set; }
int f2 { get; set; }
}

[Serializable()]
public class classA : A
{

private int _f1;
public int f1
{
get { return _f1; }
set { _f1 = value; }
}

private int _f2;
public int f2
{
get { return _f2; }
set { _f2 = value; }
}
}

public interface B
{
int f1 { get; set; }
int f2 { get; set; }
}

[Serializable()]
public class classb : B
{

private int _f1;
public int f1
{
get { return _f1; }
set { _f1 = value; }
}

private int _f2;
public int f2
{
get { return _f2; }
set { _f2 = value; }
}
}


}


通过序列化和反序列化来转化
机器人 2011-03-24
  • 打赏
  • 举报
回复
不过 ClassB 中需要显示实现接口。

-------------

这样也应该可以:

客户端(IB激活)--> 服务甲(ClassB:IB) --> 服务乙(ClassA:IA)
【ProxyA实现IB】
机器人 2011-03-24
  • 打赏
  • 举报
回复
有一点应该没有问题,比如:

public ClassB : MarshalByRefObject, IA, IB
{
...
}

B端可以用 IB 激活服务实例。
wllllll 2011-03-24
  • 打赏
  • 举报
回复

using System;
using System.Web.UI;
using System.Configuration;


public class ClassA {
public String Text = "";
public ClassA() {
}
public ClassA(String dText) {
this.Text = dText;
}
public static explicit operator ClassB(ClassA dValue) {
return new ClassB(dValue.Text);
}
}
public class ClassB {
public String Text = "";
public ClassB() {
}
public ClassB(String dText) {
this.Text = dText;
}
public static explicit operator ClassA(ClassB dValue) {
return new ClassA(dValue.Text);
}
}

public partial class oMenu : UserControl {
protected void Page_Load(object sender, EventArgs e) {
ClassA a = new ClassA();
ClassB b = (ClassB)a;
}
}

wllllll 2011-03-24
  • 打赏
  • 举报
回复
public class ClassA {
String Text = "";
public ClassA() {
}
public ClassA(String dText) {
this.Text = dText;
}
public static explicit operator ClassB(ClassA dValue) {
return new ClassB(dValue.Text);
}
}
public class ClassB {
String Text = "";
public ClassB() {
}
public ClassB(String dText) {
this.Text = dText;
}
public static explicit operator ClassA(ClassB dValue) {
return new ClassA(dValue.Text);
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧