如何修复C#的语法问题

本文关键字:语法 问题 何修复 | 更新日期: 2023-09-27 18:07:50

好吧,我有一些语法问题一直在回避我

一个是在类型或名称空间定义的末尾,或者预期的文件末尾

另一个是在CCD_ 1处的大约一半,具有{预期

感谢

和实际的代码:(我也把错误放在这里(

namespace Gmail_final_prep
{
    public class Gmail_FP
    {
        private static System.Timers.Timer gTimer;
        public static void GMain()
        {
            gTimer = new System.Timers.Timer(2000);
            // Hook up the Elapsed event for the timer.
            gTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent1);
            gTimer.Interval = 2000;
            gTimer.Enabled = true;
            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();
            // If the timer is declared in a long-running method, use
            // KeepAlive to prevent garbage collection from occurring
            // before the method ends.
            //GC.KeepAlive(gTimer);
        }
        private static void OnTimedEvent1(object source, ElapsedEventArgs e)
        { // { expected here
            public static string TextToBase64(string sAscii)
            {
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] bytes = encoding.GetBytes(sAscii);
                return System.Convert.ToBase64String(bytes, 0, bytes.Length);
            }
            public static string CheckMail()
            {
                string result = "0";
                try
                {
                    var url = @"https://gmail.google.com/gmail/feed/atom";
                    var USER = "usr";
                    var PASS = "pss";
                    var encoded = TextToBase64(USER + ":" + PASS);
                    var myWebRequest = HttpWebRequest.Create(url);
                    myWebRequest.Method = "POST";
                    myWebRequest.ContentLength = 0;
                    myWebRequest.Headers.Add("Authorization", "Basic " + encoded);
                    var response = myWebRequest.GetResponse();
                    var stream = response.GetResponseStream();
                    XmlReader reader = XmlReader.Create(stream);
                    System.Text.StringBuilder gml = new System.Text.StringBuilder();
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                            if (reader.Name == "fullcount")
                            {
                                gml.Append(reader.ReadElementContentAsString()).Append(",");
                                //result = reader.ReadElementContentAsString();
                                //return result;
                            }
                    }
                    Console.WriteLine(gml.ToString());
                }
                catch (Exception ee) { Console.WriteLine(ee.Message); }
                return result;
            }
        }       
    }
} // Type or namespace definition, or end-of-file expected here

如何修复C#的语法问题

您在另一个方法中有一个方法。

    private static void OnTimedEvent1(object source, ElapsedEventArgs e) 
    { // { expected here 
        public static string TextToBase64(string sAscii)

这可能是您想要做的事情——您希望从事件处理程序中调用CheckMail()

using System;
using System.Net;
using System.Timers;
using System.Xml;
namespace Gmail_final_prep 
{ 
public class Gmail_FP 
{ 
    private static System.Timers.Timer gTimer; 
    public static void GMain() 
    { 
        gTimer = new System.Timers.Timer(2000); 
        // Hook up the Elapsed event for the timer. 
        gTimer.Elapsed += OnTimedEvent1; 
        gTimer.Interval = 2000; 
        gTimer.Enabled = true; 
        Console.WriteLine("Press the Enter key to exit the program."); 
        Console.ReadLine(); 
        // If the timer is declared in a long-running method, use 
        // KeepAlive to prevent garbage collection from occurring 
        // before the method ends. 
        //GC.KeepAlive(gTimer); 
    } 
    private static void OnTimedEvent1(object source, ElapsedEventArgs e) 
    { // { expected here 
        CheckMail();
    }
    public static string TextToBase64(string sAscii)
    {
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
        byte[] bytes = encoding.GetBytes(sAscii);
        return System.Convert.ToBase64String(bytes, 0, bytes.Length);
    }
    public static string CheckMail()
    {
        string result = "0";
        try
        {
            var url = @"https://gmail.google.com/gmail/feed/atom";
            var USER = "usr";
            var PASS = "pss";
            var encoded = TextToBase64(USER + ":" + PASS);
            var myWebRequest = HttpWebRequest.Create(url);
            myWebRequest.Method = "POST";
            myWebRequest.ContentLength = 0;
            myWebRequest.Headers.Add("Authorization", "Basic " + encoded);
            var response = myWebRequest.GetResponse();
            var stream = response.GetResponseStream();
            XmlReader reader = XmlReader.Create(stream);
            System.Text.StringBuilder gml = new System.Text.StringBuilder();
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                    if (reader.Name == "fullcount")
                    {
                        gml.Append(reader.ReadElementContentAsString()).Append(",");
                        //result = reader.ReadElementContentAsString(); 
                        //return result; 
                    }
            }
            Console.WriteLine(gml.ToString());
        }
        catch (Exception ee) { Console.WriteLine(ee.Message); }
        return result;
    } 
} 
} // Type or namespace definition, or end-of-file expected here 
    private static void OnTimedEvent1(object source, ElapsedEventArgs e)
    { // { expected here
        public static string TextToBase64(string sAscii)
        {

这是不合法的——你不能把一个方法嵌入到另一个方法中。

我想知道这个部分在做什么:

private static void OnTimedEvent1(object source, ElapsedEventArgs e)
{ // { expected here
     public static string TextToBase64(string sAscii) 

你的问题可能就在这里。