你能猜出在泛型中不允许使用密封类进行类型约束的原因是什么?我只有一个解释是给机会使用裸约束.
我有非常简单的代码,对我来说很好:
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) 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) 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) 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) 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# ×6
.net ×2
algorithm ×1
asp.net-mvc ×1
clr ×1
constraints ×1
events ×1
generics ×1
linq ×1
performance ×1
string ×1