有没有什么办法可以继承一个类,而不在.NET构造函数? [英] Is there any way to inherit a class without constructors in .NET?

查看:156
本文介绍了有没有什么办法可以继承一个类,而不在.NET构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在修改一些HttpWebRequest的功能,但通过继承因为HttpWebRequest的没有公共构造函数(除了反序列化构造函数),我不能这样做。是否有解决办法做到这一点?

我的目标是code像下面的例子,但该类对象必须继承HttpWebRequest的属性和方法:

 使用系统;
使用System.Net;
使用的System.Threading;

公共类AsyncWebRequest WebRequest类
{
    私人只读的AsyncCallback getResponseCallback;
    私人只读乌里URI;
    私人挥发性INT RetriesLeft = 3;
    私人挥发性的WebRequest请求;

    公共AsyncWebRequest(字符串URI,AsyncCallback的getResponseCallback)
       :这(新的URI(URI),getResponseCallback)
    {
    }

    公共AsyncWebRequest(URI URI,AsyncCallback的getResponseCallback):基地()
    {
        this.uri = URI;
        this.getResponseCallback = getResponseCallback;
    }

    私人IAsyncResult的BeginGetResponse()
    {
        请求= HttpWebRequest.CreateDefault(URI);
        ((HttpWebRequest的)要求).ReadWriteTimeout =超时;
        VAR的结果= request.BeginGetResponse(GetResponseCallback,NULL);
        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
            GetResponseTimeout,空,超时,真正的);
        返回结果;
    }

    私人无效GetResponseTimeout(对象状态,布尔TIMEDOUT)
    {
        如果(TIMEDOUT)
        {
            重试();
        }
    }

    私人无效重试()
    {
        request.Abort();
        布尔重试= FALSE;
        锁(要求)
        {
            如果(RetriesLeft大于0)
            {
                Interlocked.Decrement(REF RetriesLeft);
                重试= TRUE;
            }
        }
        如果(重试)
        {
            BeginGetResponse();
        }
        其他
        {
            getResponseCallback(空);
        }
    }

    私人无效GetResponseCallback(IAsyncResult的AsyncResult)
    {
        尝试
        {
            getResponseCallback(AsyncResult);
        }
        赶上(WebException webException)
        {
            重试();
        }
    }
}
 

解决方案

除非你可以欺骗序列化的构造做您的出价,那么没有,也没办法。

这是类的构造函数是内部的,所以你没有办法打电话给他们的。

I'm currently trying to modify some HttpWebRequest functions, but I can't do it through inheritance because HttpWebRequest has no public constructors (besides the deserialization constructor). Is there a workaround to do this?

My objective is to code something like the example below, but this class objects must inherit the HttpWebRequest properties and methods:

using System;
using System.Net;
using System.Threading;

public class AsyncWebRequest:WebRequest
{
    private readonly AsyncCallback getResponseCallback;
    private readonly Uri uri;
    private volatile int RetriesLeft = 3;
    private volatile WebRequest request;

    public AsyncWebRequest(string uri, AsyncCallback getResponseCallback)
       :this(new Uri(uri), getResponseCallback)
    {
    }

    public AsyncWebRequest(Uri uri, AsyncCallback getResponseCallback):base()
    {
        this.uri = uri;
        this.getResponseCallback = getResponseCallback;
    }

    private IAsyncResult BeginGetResponse()
    {
        request = HttpWebRequest.CreateDefault(uri);
        ((HttpWebRequest)request).ReadWriteTimeout = Timeout;
        var result = request.BeginGetResponse(GetResponseCallback, null);
        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
            GetResponseTimeout, null, Timeout, true);
        return result;
    }

    private void GetResponseTimeout(object state, bool timedOut)
    {
        if (timedOut)
        {
            Retry();
        }
    }

    private void Retry()
    {
        request.Abort();
        bool retry = false;
        lock (request)
        {
            if (RetriesLeft > 0)
            {
                Interlocked.Decrement(ref RetriesLeft);
                retry = true;
            }
        }
        if (retry)
        {
            BeginGetResponse();
        }
        else
        {
            getResponseCallback(null);
        }
    }

    private void GetResponseCallback(IAsyncResult AsyncResult)
    {
        try
        {
            getResponseCallback(AsyncResult);
        }
        catch(WebException webException)
        {
            Retry();
        }
    }
}

解决方案

Unless you can trick the serialization constructor to do your bidding, then no, there is no way.

The constructors of that class are internal, so you have no way of calling them.

这篇关于有没有什么办法可以继承一个类,而不在.NET构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆