This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

[.NET FAQ NO2010112201]“没有找到适合的方法来重写”解决方案

196 views 0 comments last modified about 8 years ago Raymond Tang

In this page

P.S. 从今天开始,我会将发布一系列.NET FAQ的文章,主要收集在开发.NET程序中经常遇到的一些问题以及它们的解决方案。

今天的问题是:没有找到适合的方法来重写

A问题来源

http://social.msdn.microsoft.com/Forums/zh-CN/2212/thread/2c9493b4-95a5-4d45-a336-54e9c877ecbf
在MSDN论坛上有朋友提出这样的问题。
B问题易出现人群
学习新手,对VS不熟悉,操作编码习惯不合理等

C问题出现原因:
使用了override关键字重写了基类(继承的父类)中不存在的方法就会出现这样的情况。参考使用Override和New关键字进行版本控制override关键字

D问题再现与解决方案:
namespace WindowsApplication1
{
partial class Form1
{
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
}
在添加一个Form的时候比如这里的Form1,VS会自动生成界面呈现等等相关的代码,以及这里的Dispose方法

 

之所以会出现这个问题是因为Form2类并没有继承自提供了Dispose的可重写方法的父类

怎样会出现这样的情况呢?

接着这个例子,在生成上面这段代码的同时,还会生成

public class Form1:Form

这样在编译的时候会合成为一个类Form1,如果你不清楚部分类partial是什么概念,请查看msdn上的partial这个关键字。

但是如果将Form1的类名进行了修改,或者将其命名空间进行了修改比如:

namespace WindowsApplication2
{
public class Form1:Form
{
//...
}
}

那么部分类Form1就成了孤立的类了,而他只继承自object,但是object类是没有可重写的Dispose方法的 那么就会出现这样的编译时错误了。

因此解决的方案,即是确保继承的类包含override重写的方法,而在操作上则是不要随意去修改code-behind的类签名(命名空间与名称等),即便需要修改,也需确保VS自动生成的类也做了相应的修改

在ASP.NET中也有人遇到类似的问题诸如GetTypeHashCode()没有找到适合的方法来重写,解决的途径与此类似。


Related pages

Tuples in C# (4.x, 7.0, 7.1)

92 views   0 comments last modified about 4 months ago

What is a tuple? Tuple is an ordered sequence, fixed-size, immutable and of heterogeneous objects. Tuple has been existing in other languages such as F#, Python, Perl and etc. for a long time. It was first introduced into C# from C# 4.0 and has been evolving over time. Since C# 7.1, tuple...

View detail

Invoke Hadoop WebHDFS APIs in .NET Core

141 views   0 comments last modified about 4 months ago

Background Apache doesn't provide native official .NET APIs for Hadoop HDFS. The HTTP REST API supports the complete FileSystem / ...

View detail

Logging configuration in .NET core

273 views   0 comments last modified about 9 months ago

.NET core introduces a logging API that works with a number of logging frameworks. The built-in providers are configurable and extensible to support different level loggings.

View detail

Sending Emails in .NET Core Applications

1216 views   0 comments last modified about 9 months ago

Sending emails are common in applications. For example, when user registers, we need to send account activation emails. This post summarize the approaches we can use to send emails in .NET Core 1.x and 2.x.

View detail

Retrieve Http client request metadata like IP address and languages in asp.net core

833 views   0 comments last modified about 9 months ago

IP Address In ASP.NET Core, Request.UserHostAddress has been removed though that attribute exists in the traditional ASP.NET applications. We can use HttpContext.Connection to retrieve the remove client IP address: var ipAddress = HttpContext.Connecti...

View detail

Migrating from ASP.NET Core 1.x to ASP.NET Core 2.0

883 views   2 comments last modified about 10 months ago

Migrating from ASP.NET Core 1.x to 2.0 is not an easy job especially if you have customized Identity and used customized authentication. This post summarizes the issues and errors I have experienced and their resolutions when upgrading my project. Hopefully it can save you sometime if you are doi...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.