Use the extensions method in C #2.0

Source: Internet
Author: User

Use the extensions method in C #2.0

I recently tested an FTP class and found that it cannot be compiled in. netcf V2, but it can be compiled in version 3.0. The error message is as follows:

 
Using system; using system. runtime. compilerservices; public static class extensions {public static datetime? Todatetime (this winapi. filetime time) {If (time. dwhighdatetime = 0) & (time. dwlowdatetime = 0) {return NULL;} uint dwlowdatetime = (uint) time. dwlowdatetime; long filetime = (time. dwhighdatetime <0x20) | dwlowdatetime; return New datetime? (Datetime. fromfiletimeutc (filetime ));}}

Later, I found out that it was in. netcf V3.0.Extension Method feature.One major purpose of extension method is to construct an auxiliary method.

To solve this problem, you can use either of the following methods:

Method 1: add the references of system. Core. DLL to the existing project. In this way, no errors are reported during compilation, and the test is normal. However, each project contains a system. Core. dll file, which is uncomfortable.

Method 2: add the following to the project:Code:

Namespace system. runtime. compilerservices {[attributeusage (attributetargets. Assembly | attributetargets. Class | attributetargets. Method)] public sealed class extensionattribute: attribute {}}

After the code is added, no error is reported during compilation and the code runs normally. You do not need to add the. netcf V3.0 system. Core. dll file.

During data query,Use C #3.0 extensions
Method"This articleArticleGood.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.