c# 代理的一个例子

 using System;
using System.Collections.Generic;
using System.Text;

namespace DelegateApp
{
   
    class DelegateClass
    {
        delegate void Del(string Name);//声明一个代理
        public void F1(string Name)
        {
            Console.WriteLine("CNN "+Name);
        }
        public void F2(string Name)
        {
            Console.WriteLine("Carefare " + Name);
        }
        public void F3(string Name)
        {
            Console.WriteLine("Hondar " + Name);
        }
        public void Print()
        {
            Del f = (new Del(F1)) + (new Del(F2)) + (new Del(F1));//代理注册
            f("La");//代理调用
        }
    }

    class Program
    {

        static void Main(string[] args)
        { DelegateClass d = new DelegateClass();
        d.Print();
    }
    }
}

好的,下面是一个简单的 C# WCF 服务的例子: 首先,在 Visual Studio 中创建一个新的 WCF 服务应用程序项目。然后,在服务契约中定义一个简单的操作: ```C# [ServiceContract] public interface IMyService { [OperationContract] string GetMessage(string name); } ``` 这个操作接受一个字符串类型的参数,返回一个字符串类型的消息。 接下来,在服务实现中实现这个操作: ```C# public class MyService : IMyService { public string GetMessage(string name) { return "Hello, " + name + "!"; } } ``` 这个实现接受一个字符串类型的参数,返回一个拼接了该参数的消息。 然后,在配置文件中定义一个终结点: ```XML <system.serviceModel> <services> <service name="MyService"> <endpoint address="http://localhost:8080/MyService" binding="basicHttpBinding" contract="IMyService"/> </service> </services> </system.serviceModel> ``` 这个终结点定义了服务的地址、绑定和契约等信息。 最后,在 host 中启动服务: ```C# ServiceHost host = new ServiceHost(typeof(MyService)); host.Open(); ``` 这个 host 启动了 MyService 类型的服务。 现在,WCF 服务已经启动并且可以接收客户端的请求。在客户端中,你可以使用类似下面的代码来调用服务: ```C# ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>( new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/MyService")); IMyService proxy = factory.CreateChannel(); string message = proxy.GetMessage("World"); Console.WriteLine(message); ``` 这个客户端创建了一个 IMyService 类型的代理对象,并调用了它的 GetMessage 方法,将 "World" 作为参数传递,并将返回的消息打印到控制台上。 这就是一个简单的 C# WCF 服务的例子,它展示了如何定义服务契约、实现服务操作、配置服务终结点和启动服务 host。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值