几天来,我一直在head头,但我仍然不明白如何实现此接口.
I have been scratching my head over this for days and I still cannot understand how to implement this interface.
这是我的代码:
namespace ConsoleApplication32 { public static class ScanAndSerialize { public static void Serialize() { List<string> dirs = FileHelper.GetFilesRecursive("s:\\"); List<string> dirFiles = new List<string>(); foreach (string p in dirs) { string path = p; string lastAccessTime = File.GetLastAccessTime(path).ToString(); bool DirFile = File.Exists(path); DateTime lastWriteTime = File.GetLastWriteTime(p); //dirFiles.Add(p + " , " + lastAccessTime.ToString() + " , " + DirFile.ToString() + " , " + lastWriteTime.ToString()); dirFiles.Add(p); dirFiles.Add(lastAccessTime); dirFiles.Add(DirFile.ToString()); dirFiles.Add(lastWriteTime.ToString()); dirFiles.Add(Environment.NewLine); } XmlSerializer SerializeObj = new XmlSerializer(dirFiles.GetType()); string sDay = DateTime.Now.ToString("MMdd"); string fileName = string.Format(@"s:\project\{0}_file.xml", sDay); TextWriter WriteFileStream = new StreamWriter(fileName); SerializeObj.Serialize(WriteFileStream, dirFiles); WriteFileStream.Close(); } static class FileHelper { public static List<string> GetFilesRecursive(string b) { // 1. // Store results in the file results list. List<string> result = new List<string>(); // 2. // Store a stack of our directories. Stack<string> stack = new Stack<string>(); // 3. // Add initial directory. stack.Push(b); // 4. // Continue while there are directories to process while (stack.Count > 0) { // A. // Get top directory string dir = stack.Pop(); try { // B // Add all files at this directory to the result List. result.AddRange(Directory.GetFiles(dir, "*.*")); // C // Add all directories at this directory. foreach (string dn in Directory.GetDirectories(dir)) { stack.Push(dn); } } catch { // D // Could not open the directory } } return result; } } public class MyInterface: IValidationRowSet { public int RowNumber { get; set; } public string RowAsString { get; set; } public IValidationRowSet MatchedRow { get; set; } public string FriendlyNameLabel { get; set; } public string KeyFieldLabel { get; set; } IList<string> lst = new List<string>(); public string SourceWorksheetName { get; set; } public string SourceRangeName { get; set; } //public string SourceRangeName { get; set; } public bool bReported { get; set; } public int FieldCount { get { return lst.Count; } } public string FieldData(int id) { if (id <= lst.Count) return lst[id]; else return null; } public string ValidationMessage { get; set; } }这里是界面的说明(仍然为我着迷)
Here is an explanation of the interface (still scratching my head over this one)
namespace Validation { /// <summary> /// Implement this interface if you want the engine to callback when it finds exception /// messages. You will pass a reference to you class to the validation engine, and /// it will call "PostValidationMessage" for each exception example, including the message, /// the entire row set of data (vr), and the id of the field that created the exception. /// </summary> public interface IValidationReporter { /// <param name="sMsg"></param> /// <param name="vr"></param> /// <param name="id"></param> void PostValidationMessage(string sMsg, IValidationRowSet vr, int id); } /// <summary> /// Implement this interface in order to use the validation engine. /// The validation engine takes 2 IList<IValidationRowSet> objects and compares them. /// A class that implements this interface will contain an entire row of data that you'll /// want to compare. /// </summary> public interface IValidationRowSet { /// <summary> /// should return an int of the number of fields in this row /// </summary> int FieldCount { get; } /// <summary> /// should return an int of the row number that this row is in the set /// usually set when the data is assembled /// </summary> int RowNumber { get; set; } /// <summary> /// this is a function that should return the field data for this row at zero-indexed location "id" /// ex: if the row contains this data: smith|fred|2126782524|fred@smith| /// a call on this method of FieldData(2) will return the phone number 2126782524 /// </summary> /// <param name="id"></param> /// <returns></returns> string FieldData(int id); /// <summary> /// this will be modified by the validation process /// </summary> string ValidationMessage { get; set; } /// <summary> /// this will be modified by the validation process /// </summary> IValidationRowSet MatchedRow { get; set; } /// <summary> /// returns a string that uniquely identifies this row /// ex: if the row contains this data: smith|fred|2126782524|fred@smith| /// so for this example, the unique identifier could be the email address fred@smith /// </summary> string KeyFieldLabel { get; set; } /// <summary> /// returns a string with the "friendly" name of this row /// ex: if the row contains this data: smith|fred|2126782524|fred@smith| /// so for this example, FriendlyNameLabel could be the name, such as "Fred Smith" /// </summary> string FriendlyNameLabel { get; set; } /// <summary> /// returns all fields in the row as pipe delimited /// ex: 1,234.23|Fred Smith|Fred@smith| /// </summary> string RowAsString { get; set; } /// <summary> /// if this is an excel file comparison, this should return the name /// of the worksheet from whence this data came /// </summary> string SourceWorksheetName { get; set; } /// <summary> /// if this is an excel file comparison, this should return the name /// of the worksheet range from whence this data came /// </summary> string SourceRangeName { get; set; } /// <summary> /// this will be modified by the validation process /// </summary> bool bReported { get; set; } } }我已经阅读了许多有关Interfaces的文章/书籍/论坛.这个概念对我来说就像是一个黑洞……而我在一个必须实现这一点的项目中.有人知道您如何实现此目标吗?顺便说一句-我是一个完整的新手程序员...不到2个月的经验...因此,请不要为我的环保而责备我.
I have read NUMEROUS articles/books/forum postings about Interfaces. This concept feels like a black hole to me...and i'm on a project where i have to implement this. Anybody have ANY idea how the heck you implement this? By the way--i'm a COMPLETE newbie programmer...less than 2 months experience...therefore please do not chastise me for my green-ness please.
谢谢.
推荐答案请考虑将接口作为要填写的难题的原型或模板-将其视为空白,并将线条放在其中.您将必须将接口派生为具体的类-漂亮的拼图游戏.
Consider interfaces to be a prototype or a template for a puzzle to be filled in - think of them as white space and lines where to put the pieces. You will have to derive the interfaces into concrete classes - the pretty picture puzzle.
让我保存一下,然后举一个例子.
Let me save this and I'll put up an example.
interface IFoo { bool DoFoo(int number); } class Foo : IFoo { public bool DoFoo(int number) { return (number++ >= 0); } } class Foo2 : IFoo { public bool DoFoo(int number) { return (number-- >= 0); } }现在我已经拥有了,我可以做这样的事情.
Now that I have that, I can do stuff like this.
IFoo foo; if (!value) foo = new Foo(); else foo = new Foo2(); bool value2 = foo.DoFoo(27);注意,我无法通过接口执行此操作
Notice, I cannot do this with interfaces:
// WRONG Foo2 foo2 = new Foo();因此,基本上可以概括出接口的功能及其工作方式.您现在的工作是实施这些接口的具体实现.
So that basically sums up what an interface does and how it works. What your job now is to implement those concrete implementations of the interface.
更多推荐
在代码中实现接口
发布评论