mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
Change license to MIT
This commit is contained in:
parent
4b965f0064
commit
78dc44643f
@ -1,34 +1,39 @@
|
|||||||
// Copyright 2011 Kevin Glynn (http://www.twigletsoftware.com)
|
// Copyright 2011 Kevin Glynn (http://www.twigletsoftware.com)
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// The MIT License (MIT)
|
||||||
// you may not use this file except in compliance with the License.
|
//
|
||||||
// You may obtain a copy of the License at
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||||
|
// and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||||
|
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
// subject to the following conditions:
|
||||||
//
|
//
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
// The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||||
//
|
// portions of the Software.
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
// See the License for the specific language governing permissions and
|
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
// limitations under the License.
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
namespace Twiglet.Sample.Delegate
|
|
||||||
{
|
namespace Twiglet.Sample.Delegate
|
||||||
|
{
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sample class to show off CS2J's translations for delegates and events
|
/// Sample class to show off CS2J's translations for delegates and events
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DelegateSampler
|
public class DelegateSampler
|
||||||
{
|
{
|
||||||
public delegate void xProcessor<T>(ref T value);
|
public delegate void xProcessor<T>(ref T value);
|
||||||
//
|
//
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// LogWriters take a string and record it.
|
/// LogWriters take a string and record it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logMessage">the message to be recorded</param>
|
/// <param name="logMessage">the message to be recorded</param>
|
||||||
public delegate void LogWriter(string logMessage);
|
public delegate void LogWriter(string logMessage);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -38,7 +43,7 @@ namespace Twiglet.Sample.Delegate
|
|||||||
/// However the delegates can communictae if we use ref parameters.
|
/// However the delegates can communictae if we use ref parameters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="value">the variable that we are processing</param>
|
/// <param name="value">the variable that we are processing</param>
|
||||||
public delegate void Processor<T>(ref T value);
|
public delegate void Processor<T>(ref T value);
|
||||||
|
|
||||||
// This variable is captured by the delegates, if we change this variable
|
// This variable is captured by the delegates, if we change this variable
|
||||||
// then it changes what they print.
|
// then it changes what they print.
|
||||||
@ -52,33 +57,33 @@ namespace Twiglet.Sample.Delegate
|
|||||||
private void MethodDelegateTwo(string s)
|
private void MethodDelegateTwo(string s)
|
||||||
{
|
{
|
||||||
Console.WriteLine("MethodDelegateTwo[" + _captured_string + "]:\t\t" + s);
|
Console.WriteLine("MethodDelegateTwo[" + _captured_string + "]:\t\t" + s);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RunIt() {
|
public void RunIt() {
|
||||||
|
|
||||||
// First we create some delegates using the many syntaxes supported by C#
|
// First we create some delegates using the many syntaxes supported by C#
|
||||||
|
|
||||||
// Old fashioned delegate creation
|
// Old fashioned delegate creation
|
||||||
// initialize delegate with a named method.
|
// initialize delegate with a named method.
|
||||||
LogWriter delA = new LogWriter(MethodDelegate);
|
LogWriter delA = new LogWriter(MethodDelegate);
|
||||||
|
|
||||||
// We can just also just assign a method group to a delegate variable
|
// We can just also just assign a method group to a delegate variable
|
||||||
LogWriter delB = MethodDelegateTwo;
|
LogWriter delB = MethodDelegateTwo;
|
||||||
|
|
||||||
// Since C# 2.0 a delegate can be initialized with
|
// Since C# 2.0 a delegate can be initialized with
|
||||||
// an "anonymous method."
|
// an "anonymous method."
|
||||||
LogWriter delC = delegate(string s) { Console.WriteLine("AnonymousMethodDelegate[" + _captured_string + "]:\t\t" + s); };
|
LogWriter delC = delegate(string s) { Console.WriteLine("AnonymousMethodDelegate[" + _captured_string + "]:\t\t" + s); };
|
||||||
|
|
||||||
// Since C# 3.0 a delegate can be initialized with
|
// Since C# 3.0 a delegate can be initialized with
|
||||||
// a lambda expression.
|
// a lambda expression.
|
||||||
LogWriter delD = (string s) => { Console.WriteLine("LambdaExpressionDelegate[" + _captured_string + "]:\t\t" + s); };
|
LogWriter delD = (string s) => { Console.WriteLine("LambdaExpressionDelegate[" + _captured_string + "]:\t\t" + s); };
|
||||||
|
|
||||||
// Since C# 3.0 a delegate can be initialized with
|
// Since C# 3.0 a delegate can be initialized with
|
||||||
// a lambda expression, the type of the argument is inferred by the compiler.
|
// a lambda expression, the type of the argument is inferred by the compiler.
|
||||||
LogWriter delE = s => { Console.WriteLine("InferredLambdaExpressionDelegate[" + _captured_string + "]:\t" + s); };
|
LogWriter delE = s => { Console.WriteLine("InferredLambdaExpressionDelegate[" + _captured_string + "]:\t" + s); };
|
||||||
|
|
||||||
// Invoke the delegates.
|
// Invoke the delegates.
|
||||||
delA("Peter Piper");
|
delA("Peter Piper");
|
||||||
delB("picked a peck");
|
delB("picked a peck");
|
||||||
delC("of pickled peppers.");
|
delC("of pickled peppers.");
|
||||||
delD("A peck of pickled peppers");
|
delD("A peck of pickled peppers");
|
||||||
@ -111,65 +116,65 @@ namespace Twiglet.Sample.Delegate
|
|||||||
int val = 5;
|
int val = 5;
|
||||||
calcIt(ref val);
|
calcIt(ref val);
|
||||||
Console.WriteLine("(4 * (5^5)) + 1 = " + val);
|
Console.WriteLine("(4 * (5^5)) + 1 = " + val);
|
||||||
}
|
}
|
||||||
|
|
||||||
delegate void DelC (string s);
|
delegate void DelC (string s);
|
||||||
|
|
||||||
static void Hello (string s)
|
static void Hello (string s)
|
||||||
{
|
{
|
||||||
System.Console.WriteLine (" Hello, {0}!", s);
|
System.Console.WriteLine (" Hello, {0}!", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Goodbye (string s)
|
static void Goodbye (string s)
|
||||||
{
|
{
|
||||||
System.Console.WriteLine (" Goodbye, {0}!", s);
|
System.Console.WriteLine (" Goodbye, {0}!", s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HelloGoodbye ()
|
void HelloGoodbye ()
|
||||||
{
|
{
|
||||||
DelC a, b, c, d;
|
DelC a, b, c, d;
|
||||||
|
|
||||||
// Create the delegate object a that references
|
// Create the delegate object a that references
|
||||||
// the method Hello:
|
// the method Hello:
|
||||||
a = Hello;
|
a = Hello;
|
||||||
|
|
||||||
// Create the delegate object b that references
|
// Create the delegate object b that references
|
||||||
// the method Goodbye:
|
// the method Goodbye:
|
||||||
b = Goodbye;
|
b = Goodbye;
|
||||||
|
|
||||||
// The two delegates, a and b, are composed to form c:
|
// The two delegates, a and b, are composed to form c:
|
||||||
c = a + b;
|
c = a + b;
|
||||||
|
|
||||||
// Remove a from the composed delegate, leaving d,
|
// Remove a from the composed delegate, leaving d,
|
||||||
// which calls only the method Goodbye:
|
// which calls only the method Goodbye:
|
||||||
d = c - a;
|
d = c - a;
|
||||||
|
|
||||||
System.Console.WriteLine ("Invoking delegate a:");
|
System.Console.WriteLine ("Invoking delegate a:");
|
||||||
a ("A");
|
a ("A");
|
||||||
System.Console.WriteLine ("Invoking delegate b:");
|
System.Console.WriteLine ("Invoking delegate b:");
|
||||||
b ("B");
|
b ("B");
|
||||||
System.Console.WriteLine ("Invoking delegate c:");
|
System.Console.WriteLine ("Invoking delegate c:");
|
||||||
c ("C");
|
c ("C");
|
||||||
System.Console.WriteLine ("Invoking delegate d:");
|
System.Console.WriteLine ("Invoking delegate d:");
|
||||||
d ("D");
|
d ("D");
|
||||||
|
|
||||||
|
|
||||||
d += c;
|
d += c;
|
||||||
|
|
||||||
d += c;
|
d += c;
|
||||||
|
|
||||||
d -= c;
|
d -= c;
|
||||||
|
|
||||||
System.Console.WriteLine ("Invoking composed delegate d:");
|
System.Console.WriteLine ("Invoking composed delegate d:");
|
||||||
d ("DA");
|
d ("DA");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void DelegateSamplerMain()
|
public static void DelegateSamplerMain()
|
||||||
{
|
{
|
||||||
DelegateSampler sampler = new DelegateSampler ();
|
DelegateSampler sampler = new DelegateSampler ();
|
||||||
sampler.RunIt();
|
sampler.RunIt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user