小编Gri*_*uev的帖子

为什么我们不能使用密封类作为通用约束?

你能猜出在泛型中不允许使用密封类进行类型约束的原因是什么?我只有一个解释是给机会使用裸约束.

.net c# generics constraints

22
推荐指数
2
解决办法
4980
查看次数

PrepareRequestUserAuthorizationAsync失败

我有非常简单的代码,对我来说很好:

var url = System.Web.HttpContext.Current.Request.Url;
Uri callbackUrl = new System.Uri(url, "oAuth2CallBack");

var ub = new UriBuilder(callbackUrl);
// decodes urlencoded pairs from uri.Query to var
var httpValueCollection = HttpUtility.ParseQueryString(callbackUrl.Query);
httpValueCollection.Add(UrlArguments.Param, null);

// urlencodes the whole HttpValueCollection
ub.Query = httpValueCollection.ToString();

var authorizationRequest = OAuthClient.PrepareRequestUserAuthorization(new[] { "somedata" }, ub.Uri);
authorizationRequest.Send();
Run Code Online (Sandbox Code Playgroud)

我更新了OAuth的NuGet包并以这种方式重写代码:

var url = System.Web.HttpContext.Current.Request.Url;
Uri callbackUrl = new System.Uri(url, "oAuth2CallBack");


var ub = new UriBuilder(callbackUrl);
// decodes urlencoded pairs from uri.Query to var
var httpValueCollection = HttpUtility.ParseQueryString(callbackUrl.Query);
httpValueCollection.Add(UrlArguments.Param, null);

// urlencodes the …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc dotnetopenauth asp.net-mvc-5

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

从链中删除委托

class Program
{
    internal delegate int CallBack(int i);

    static void Main(string[] args)
    {
        CallBack callbackMethodsChain = null;
        CallBack cbM1 = new CallBack(FirstMethod);
        CallBack cbM2 = new CallBack(SecondMethod);

        callbackMethodsChain += cbM1;
        callbackMethodsChain += cbM2;

        Delegate.Remove(callbackMethodsChain, cbM1);
    /*L_0039: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class  [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
        L_003e: pop 
        L_003f: ldloc.0 */

        Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);
        //Output: 2 **WTF!!!**


        callbackMethodsChain -= cbM1;
        /*
    L_0054: call class [mscorlib]System.Delegate [mscorlib]System.Delegate::Remove(class   [mscorlib]System.Delegate, class [mscorlib]System.Delegate)
          L_0059: castclass Generics.Program/CallBack
          L_005e: stloc.0 
          L_005f: ldloc.0 
        */
        Trace.WriteLine(callbackMethodsChain.GetInvocationList().Length);
        //Output: 1
    }

    private static int FirstMethod(int …
Run Code Online (Sandbox Code Playgroud)

.net c# clr

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

我实施KMP算法有什么问题?

static void Main(string[] args)
{
    string str = "ABC ABCDAB ABCDABCDABDE";//We should add some text here for 
                                           //the performance tests.

    string pattern = "ABCDABD";


    List<int> shifts = new List<int>();

    Stopwatch stopWatch = new Stopwatch();

    stopWatch.Start();
    NaiveStringMatcher(shifts, str, pattern);
    stopWatch.Stop();
    Trace.WriteLine(String.Format("Naive string matcher {0}", stopWatch.Elapsed));

    foreach (int s in shifts)
    {
        Trace.WriteLine(s);
    }

    shifts.Clear();
    stopWatch.Restart();

    int[] pi = new int[pattern.Length];
    Knuth_Morris_Pratt(shifts, str, pattern, pi);
    stopWatch.Stop();
    Trace.WriteLine(String.Format("Knuth_Morris_Pratt {0}", stopWatch.Elapsed));

    foreach (int s in shifts)
    {
        Trace.WriteLine(s);
    }

    Console.ReadKey();
}

static IList<int> NaiveStringMatcher(List<int> …
Run Code Online (Sandbox Code Playgroud)

c# string algorithm performance

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

C#事件实现(文章与反射器)

public class EventsType {public event EventHandler> NewEvent;

    public void SmthHappened(string data)
    {
        MyEventArgs<Object> eventArgs = new MyEventArgs<Object>(data);
        OnNewEvent(eventArgs);
    }

    private void OnNewEvent(MyEventArgs<Object> eventArgs)
    {
        EventHandler<MyEventArgs<Object>> tempEvent = NewEvent;

        if (tempEvent != null)
        {                
            tempEvent(this, eventArgs);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望C#编译器会像这样翻译NewEvent:

private EventHandler<MyEventArgs<object>> _newEvent;

public event EventHandler<MyEventArgs<object>> NewEvent
{
    [MethodImpl(MethodImplOptions.Synchronized)]
    add
    {
    _newEvent = (EventHandler<MyEventArgs<object>>)Delegate.Combine(_newEvent, value);
    }
    [MethodImpl(MethodImplOptions.Synchronized)]
    remove
    {
    _newEvent = (EventHandler<MyEventArgs<object>>)Delegate.Remove(_newEvent, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

,但Reflector表示它是以这种方式实现的:

public event EventHandler<MyEventArgs<object>> NewEvent
{
    add
    {
        EventHandler<MyEventArgs<object>> handler2;
        EventHandler<MyEventArgs<object>> newEvent = this.NewEvent;
        do
        { …
Run Code Online (Sandbox Code Playgroud)

c# events

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

GroupBy由许多不同的键组成

    var expr = Data.Customers
        .GroupBy(c => c.Country, c => c.Name);

    foreach (IGrouping<Countries, string> customerGroup in expr)
    {                
        Trace.WriteLine("Country: " + customerGroup.Key);
        foreach (var item in customerGroup)
        {
            Trace.WriteLine(item);
        }
        Trace.WriteLine("");
    }
Run Code Online (Sandbox Code Playgroud)

我希望得到以下结果:

国家:意大利

保罗马可

国家:美国

James Frank

代替:

国家:意大利Paolo Marco

国家:美国James Frank Frank

如果可能,请使用标准LINQ语法.

c# linq

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