小编Enr*_*uev的帖子

C#中Class vs Structure的"开销"?

我正在做课程3354(在.NET Framework 2.0中实现系统类型和接口),据说对于简单的类,使用成员变量和函数,由于开销,最好使用结构而不是类.

我从来没有听说过这种说法,这种说法的有效性是什么?

c# struct class

14
推荐指数
3
解决办法
3069
查看次数

COM接口指导

我没有太多的COM接口,所以我有一个小问题,说我有这个代码:

[Guid("148BD528-A2AB-11CE-B11F-00AA00530503"), 
 InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IEnumWorkItems {
    [PreserveSig()]
    int Next([In] uint RequestCount, [Out] out System.IntPtr Names, 
                       [Out] out uint Fetched);
    void Skip([In] uint Count);
    void Reset();
    void Clone([Out, MarshalAs(UnmanagedType.Interface)] 
                          out IEnumWorkItems EnumWorkItems);
}
Run Code Online (Sandbox Code Playgroud)

我怎么知道"148BD528-A2AB-11CE-B11F-00AA00530503"对应于IEnumWorkItems:http://msdn.microsoft.com/en-us/library/aa380706( VS.85).aspx

就像我想知道这个界面的GUID:http://msdn.microsoft.com/en-us/library/aa381811(VS.85).aspx我在哪里可以找到它?

.net c# com interface

11
推荐指数
1
解决办法
3347
查看次数

C#,动态返回类型

我需要的是一个方法,可以在一个条件后返回一个类型(没有对象,导致不允许转换).这是一个例子:

??? right (string fullStr, int endPosition)
{
 string tmpStr = "";

 tmpStr = fullStr.Substring(endPosition);

 if(tmpStr.Length == 1)
   return tmpStr[0]; //return a char!!
 else
   return tmpStr; //return a string!!
}
Run Code Online (Sandbox Code Playgroud)

我尝试了泛型,但我只能返回进入的类型,(如果返回了char中的char,并且返回了字符串中的字符串).我试过这个:

    public static T right<T>(T stringToCheck, int endPosition)
    {
        if (typeof(T).ToString() == "System.String")
        {
            string fullString = (string)((object)stringToCheck);
            string response = "";

            response = fullString.Substring(endPosition);

            if (response.Length == 1)
            {

                return (T)((object)response[0]);
            }
            else
                return (T)((object)response);
        }

        return stringToCheck;
    }
Run Code Online (Sandbox Code Playgroud)

我不能使用类型转换(返回一个对象),不能使用ref params.

对方法的调用必须保持不变:

right(stringOrChar,int) -> returns string or char.
Run Code Online (Sandbox Code Playgroud)

谢谢

c# dynamic return-type

7
推荐指数
2
解决办法
2万
查看次数

在c#3.0中,是否可以在字符串类中添加隐式运算符?

就像是

public static class StringHelpers
{
    public static char first(this string p1)
    {
        return p1[0];
    }

    public static implicit operator Int32(this string s) //this doesn't work
    {
        return Int32.Parse(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

所以:

string str = "123";
char oneLetter = str.first(); //oneLetter = '1'

int answer = str; // Cannot implicitly convert ...
Run Code Online (Sandbox Code Playgroud)

c# implicit

7
推荐指数
1
解决办法
1350
查看次数

部分排序算法

假设我有5000万个功能,每个功能都来自磁盘.

在我的程序开始时,我处理每个功能,并根据某些条件,我应用一些修改.

在我的程序中这一点,我正在从磁盘读取一个功能,处理它并将其写回来,因为我没有足够的内存来同时打开所有5000万个功能.

现在说我要对这5000万个功能进行排序,是否有任何最佳算法可以实现这一点,因为我无法同时加载每个人?

像部分排序算法或类似的东西?

sorting algorithm

6
推荐指数
1
解决办法
1753
查看次数

HttpWebResponse登录

我正在尝试从我的.net应用程序登录Web应用程序,但由于某种原因它无法正常工作.这是登录代码:

<form action="./process-login.php" method="post">
       <table border="0" cellpadding="5" cellspacing="0">
        <tr>
          <td>Username:</td>
          <td><input type="text" size="20" name="username" value=""></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" size="20" name="password" value=""></td>
        </tr>
        <tr>
          <td><input type="submit" name="axn" value=Login></td>
        </tr>
      </table>
</form>
Run Code Online (Sandbox Code Playgroud)

这是我如何从.net做到的:

string userName = "user";
string password = "password";

string postData = "username=" + userName;
            postData += ("&password=" + password);
            postData += ("&axn=Login");

HttpWebRequest loginRequest = (HttpWebRequest)
                WebRequest.Create("http://server.com/process-login.php");

//Added following answer begin
CookieContainer CC = new CookieContainer();
loginRequest.CookieContainer = CC;
//Added following answer end

loginRequest.Method = "POST";
loginRequest.Accept …
Run Code Online (Sandbox Code Playgroud)

c# httpwebrequest httpwebresponse

5
推荐指数
1
解决办法
2360
查看次数

asp.net LinkBut​​ton HyperLink问题

我的页面上有以下两个控件:

<asp:LinkButton ID="OpenLB" runat="server" >Open</asp:LinkButton>
<asp:HyperLink ID="OpenHL" runat="server">Open</asp:HyperLink>
Run Code Online (Sandbox Code Playgroud)

我在页面加载期间设置它们,如下所示:

OpenLB.PostBackUrl = @"file:\\web\documents-emails\doc1.docx";
OpenHL.NavigateUrl = @"file:\\web\documents-emails\doc1.docx";
Run Code Online (Sandbox Code Playgroud)

OpenHL有效,它打开word文件.

OpenLB不起作用,当我点击它时,我收到一个错误弹出窗口,上面写着:

Windows Internet Explorer无法找到文件'file://web//documents-emails//doc1.docx'.确保路径或Internet地址正确无误.

它看起来像url是不同的东西,我该如何解决这个问题?

.net asp.net linkbutton hyperlink

4
推荐指数
1
解决办法
7601
查看次数

C#反射、克隆

假设我有一个包含这个方法的类 Myclass:

 public class MyClass
    {
        public int MyProperty { get; set; }

        public int MySecondProperty { get; set; }

        public MyOtherClass subClass { get; set; }

        public object clone<T>(object original, T emptyObj)
        {

            FieldInfo[] fis = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);


            object tempMyClass = Activator.CreateInstance(typeof(T));


            foreach (FieldInfo fi in fis)
            {
                if (fi.FieldType.Namespace != original.GetType().Namespace)
                    fi.SetValue(tempMyClass, fi.GetValue(original));
                else
                    fi.SetValue(tempMyClass, this.clone(fi.GetValue(original), fi.GetValue(original)));
            }

            return tempMyClass;
        }
}
Run Code Online (Sandbox Code Playgroud)

然后这个类:

public class MyOtherClass 
{
    public int MyProperty777 { get; set; }
} …
Run Code Online (Sandbox Code Playgroud)

c# reflection clone

3
推荐指数
1
解决办法
9473
查看次数

c#.net 3.5 System.Net.Mail

我没有想法!如果我这样做:

string strTo = "email1@domain.com";
string strFrom = "email1@domain.com";
string strSubject = "turn on html";


MailMessage mail = new MailMessage(strFrom, strTo, strSubject, "<u>ok!</u>");

SmtpClient smtp = new SmtpClient("127.0.0.1");

smtp.Send(mail);
Run Code Online (Sandbox Code Playgroud)

它有效,但我在outlook中看到了html标签.

如果我做:

mail.IsBodyHtml = true;
Run Code Online (Sandbox Code Playgroud)

之前

smtp.Send(mail);
Run Code Online (Sandbox Code Playgroud)

我没收到电子邮件!

我检查了Exchange邮件跟踪,消息不存在.

我检查了smtp日志,我没看到我的消息!

我查了垃圾邮件过滤器,没有留言!

我查了垃圾邮件文件夹,不是那里!

奇怪的是,如果我将strTo更改为外部电子邮件,它可以工作!!!!

在这里发疯:-(

.net c# smtpclient

2
推荐指数
1
解决办法
1457
查看次数

c#来自类的对象的gettype

我怎么能做这个工作?:

public class myClass
{
 public string first;
 public int second;
 public string third;
}

public string tester(object param)
{
 //Catch the name of what was passed not the value and return it
}

//So:
myClass mC = new myClass();

mC.first = "ok";
mC.second = 12;
mC.third = "ko";

//then would return its type from definition :
tester(mC.first) // would return : "mc.first" or "myClass.first" or "first"
//and 
tester(mC.second) // would return : "mc.second" or "myClass.second" or "second"
Run Code Online (Sandbox Code Playgroud)

c# reflection class

1
推荐指数
1
解决办法
251
查看次数