public static class SerializationExtensions
{
public static string Serialize(this object obj)
{
using (var memoryStream = new MemoryStream())
using (var reader = new StreamReader(memoryStream))
{
var serializer = new DataContractSerializer(obj.GetType());
serializer.WriteObject(memoryStream, obj);
memoryStream.Position = 0;
return reader.ReadToEnd();
}
}
public static object Deserialize(this string text, Type toType)
{
using (Stream stream = new MemoryStream())
{
var data = System.Text.Encoding.UTF8.GetBytes(text);
stream.Write(data, 0, data.Length);
stream.Position = 0;
var deserializer = new DataContractSerializer(toType);
return deserializer.ReadObject(stream);
}
}
public static T Deserialize<T>(this string text)
{
var result = text.Deserialize(typeof(T));
if (result != null)
{
return (T)result;
}
return default(T);
}
}
static void Main(string[] args) {
var text = File.ReadAllLines(@"C:\Users\jpor01\dev\grh.txt");
var file = new StreamWriter(@"C:\Users\jpor01\dev\grh-out.txt", true);
foreach (string line in text)
{
var pattern = Regex.IsMatch(line, @"\b(0x00000005)\b", RegexOptions.IgnoreCase );
if (pattern == true)
{
file.WriteLine(line);
}
}
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit.");
file.Close();
System.Console.ReadKey();
}
using System;
using System.Xml;
using System.Security.Cryptography;
using System.IO;
using System.Text;
namespace PasswordEncrypt
{
public class Scrambler
{
static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("SemiS@fe");
public static string Encrypt(string originalString)
{
if (String.IsNullOrEmpty(originalString))
{
throw new ArgumentNullException
("The string which needs to be encrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(originalString);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
public static string Decrypt(string cryptedString)
{
if (String.IsNullOrEmpty(cryptedString))
{
throw new ArgumentNullException
("The string which needs to be decrypted can not be null.");
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream
(Convert.FromBase64String(cryptedString));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
}
}
public delegate int NewDelegate(string input);
static void Main(string[] args)
{
// Delegate keyword
var myDelegate = new NewDelegate(new Program().ParseInput);
// Anonymous delegate
var myDelegate2 = new NewDelegate(delegate(string input)
{
return Int32.Parse(input);
});
// Output
Console.WriteLine("Delegate keyword - {0}", myDelegate("10"));
Console.WriteLine("Anonymous delegate keyword - {0}", myDelegate2("10"));
}
private int ParseInput(string input)
{
return Int32.Parse(input);
}
DelegateInvocation.cs
// Set second delegate to the first delegate's invocation list
myDelegate += myDelegate2;
Console.WriteLine("Delegate invocation list - {0}", myDelegate.GetInvocationList());
BuiltInDelegates.cs
Func<string, int> myFunc = i => Int32.Parse(i);
Action<int> myAction = i => Console.WriteLine(i);
Predicate<int> myPredicate = i => i == 0;
using System.Xml.Linq;
namespace LeeSoft.TestUtils.SimpleProxy
{
public interface IServiceProxy
{
XDocument Load();
}
}
ConcreteServiceProxy.cs
using log4net;
using System.Net;
using System.Xml.Linq;
namespace LeeSoft.TestUtils.SimpleProxy
{
public class ConcreteServiceProxy: BaseServiceProxy
{
public ConcreteServiceProxy(ILog log) : base(log)
{
ProxyName = "ConcreteServiceProxy";
}
public override XDocument Load()
{
var client = new WebClient { Credentials = new NetworkCredential("abc", "xyz") };
// Add a user agent header in case the requested URI contains a query.
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
var data = client.OpenRead("http://www.somedomain.com/OutgoingData/clientName/DATA.XML");
var doc = XDocument.Load(data);
LogReceipt(doc);
return doc;
}
}
}
BaseServiceProxy.cs
using log4net;
using System.Globalization;
using System.Xml.Linq;
namespace LeeSoft.TestUtils.SimpleProxy
{
public abstract class BaseServiceProxy : IServiceProxy
{
protected ILog Log;
protected string ProxyName = "BaseServiceProxy";
protected BaseServiceProxy(ILog log)
{
Log = log;
}
public abstract XDocument Load();
protected void LogReceipt(XDocument data)
{
Log.Info(string.Format(CultureInfo.InvariantCulture, "Loaded data from {0}. Content: {1}", ProxyName, data));
}
}
}