STUFF

[ Donnerstag, 03. November ]

Partial Application and Currying

public static Func<TResult> Partial<TParam1, TResult>(this Func<TParam1, TResult> func, TParam1 parameter)
{
  return ()=> func(parameter);
}

public static Func<TParam1, Func<TResult>> Curry<TParam1, TResult>(this Func<TParam1, TResult> func)
{
  return parameter => () => func(parameter);
}



public static T WithRetry<T>(this Func<T> Action)
{
  var result = Default(T);
...
return result;
}

// CALL

Func<string,string> download = url => client.DownloadString(url);

Func<string, Func<string>> downloadCurry = download.Curry();

var data = download.Partial("someurl").WithRetry();
var dataCurry = downloadCurry("someurl").WithRetry();
sasa [17:02:30] -

[ Freitag, 28. Oktober ]

Check method caller type

class MethodCaller
{
  public static bool IsCallerOfExplicitType<T>() where T : class
  {
    StackFrame frame = new StackFrame(2);
    MethodBase method = frame.GetMethod();
    var t = method?.DeclaringType;
    return t!= null && t == typeof(T);
  }
}
sasa [14:04:16] -

[ Montag, 12. September ]

Transform IQueryable<T> - Linq2SQL auf DTOs mit OData

public static class QueryableExtensions
{
   public static IQueryable<TResult> Transform<TResult>(this IQueryable source)
   {
      var resultType = typeof(TResult);
      var resultProperties = resultType.GetProperties().Where(p => p.CanWrite);
 
      ParameterExpression s = Expression.Parameter(source.ElementType, "s");
 
      var memberBindings =
         resultProperties.Select(p =>
            Expression.Bind(typeof(TResult).GetMember(p.Name)[0], Expression.Property(s, p.Name))).OfType<MemberBinding>();
 
      Expression memberInit = Expression.MemberInit(
         Expression.New(typeof(TResult)),
         memberBindings
         );
 
      var memberInitLambda = Expression.Lambda(memberInit, s);
 
      var typeArgs = new[]
      {
         source.ElementType,
         memberInit.Type
      };
 
      var mc = Expression.Call(typeof(Queryable), "Select", typeArgs, source.Expression, memberInitLambda);
 
      var query = source.Provider.CreateQuery<TResult>(mc);
 
      return query;
   }
}
 
 
 
//==============================
Verwendung:
//============================== [Mehr ...]
sasa [10:23:51] -

[ Mittwoch, 20. Juli ]

Schlankes Duplex WCF

using System.ServiceModel;

////////////////////////////////////
// Interfaces
////////////////////////////////////
[ServiceContract(Namespace = "_.services", CallbackContract = typeof(IDataOutputCallback), SessionMode = SessionMode.Allowed)]
public interface IServerWithCallback
{
   [OperationContract(IsOneWay = true)]
   void StartDataOutput();
}

[ServiceContract]
public interface IDataOutputCallback
{
   event Action<string> DataReceived;

   [OperationContract(IsOneWay = true)]
   void SendDataPacket(string data);
}

////////////////////////////////////
// Server
////////////////////////////////////
public class Server : IServerWithCallback
{
   public void StartDataOutput()
   {
      IDataOutputCallback callback = OperationContext.Current.GetCallbackChannel<IDataOutputCallback>();
      for (int i = 0; i < 10; i++)
      {
         Random r = new Random();
         int interval = r.Next(5000, 10000);
         System.Threading.Thread.Sleep(interval);
         callback.SendDataPacket("Packet " + i);
      }

      callback.SendDataPacket("Last packet is this one :)");
   }
}

////////////////////////////////////
// Client
////////////////////////////////////
public class Client : IDataOutputCallback
{
   public event Action<string> DataReceived;

   public void SendDataPacket(string data)
   {
      OnDataReceived(data);
   }

   protected virtual void OnDataReceived(string obj)
   {
      var handler = DataReceived;
      if (handler != null) handler(obj);
   }
}

////////////////////////////////////
// Server Instanz
////////////////////////////////////
ServiceHost host = new ServiceHost(typeof(Server));
host.AddServiceEndpoint(typeof(IServerWithCallback), new NetTcpBinding(), "net.tcp://localhost:9080/DataService");
host.Open();

////////////////////////////////////
// Client Instanz
////////////////////////////////////
var client = new Client();
DuplexChannelFactory<IServerWithCallback> cf =
    new DuplexChannelFactory<IServerWithCallback>(
        client,
        new NetTcpBinding(),
        new EndpointAddress("net.tcp://localhost:9080/DataService"));
IServerWithCallback srv = cf.CreateChannel();
srv.StartDataOutput();


////////////////////////////////////
// Config Endpoint Definition
////////////////////////////////////
<configuration>
  <system.serviceModel>
   
   <services>
      <service name="Server">
        <endpoint address="net.tcp://localhost:9080/DataService" binding="netTcpBinding"
            bindingConfiguration="" name="DataEndpoint" contract="IServerWithCallback" />
      </service>
    </services>
   
    <client>
      <endpoint address="net.tcp://localhost:9080/DataService" binding="netTcpBinding"
            bindingConfiguration="" contract="IServerWithCallback" name="Callback">
      </endpoint>
    </client>
   
  </system.serviceModel>
</configuration>
sasa [11:07:12] -

[ Freitag, 24. Juni ]

Tech 2 Look @

sasa [12:12:58] -

[ Dienstag, 10. Mai ]

Umlaute in Kommandozeile - Batch

Die Anzeige von Ausgaben mit Umlauten in der Kommandozeile ist häufig fehlerhaft, da die Batchdatei in einem falschen Format abgespeichert ist.
Selbst mit Notepad++ und den diversen Kodierungsoptionen kommt man zu keinem brauchbaren Ergebnis.

Folgende Schritte sind in Notepad++ erforderlich, damit die Umlaute in der Kommandozeile richtig angezeigt werden:
1. SPRACHEN --> M --> MS-DOS-STIL
2. (optional) Kodierung --> Zeichensatz --> Mitteleuropäisch --> OEM 852
3. Umlaute korrigieren
4. Datei speichern
4. (optional) SPRACHEN --> Batch (aktiviert das Syntax Highlightning) - da die Datei vorher gespeichert ändert sich das Format nicht

sasa [08:55:59] -

[ Donnerstag, 15. Oktober ]

WOW Tools


sasa [12:52:04] -

[ Montag, 07. September ]

MVC AJAX JSON

JavaScript


<script type="text/javascript">
   function check() {
      var input = $("#txtInput").val();        
 
      $.ajaxSetup({ cache: false }); // this will avoid caching - whithout this only one call will be performed
      $.ajax({
         url: "@Url.Action("TheMvcAction", "TheMvcController")",
         type: "get",
         data: { input: input },
         success: function (data) {
            alert(data.isValid);
         },
         error: function (msg) {
            alert(msg);
         },
         complete: function () {
            // this is the finally block
         }
            });
        }
    </script>
 

C# Controller


public JsonResult CheckSimPin(string input)
{
   var isValid = PerformSomeChecks(input);
   return Json(new { isValid }, JsonRequestBehavior.AllowGet);
}
 
 
sasa [14:37:47] -

CSS Umgang mit dem Layout

http://de.learnlayout.com/position.html
sasa [13:49:29] -

[ Dienstag, 25. November ]

WMIC

technet.microsoft.com Using Windows Management Instrumentation Command-line


http://blogs.technet.com/b/askperf/archive/2012/02/17/useful-wmic-queries.aspx


Wildcards
PATH win32_UsbHub where "Caption like 'Prefix%' and Type>'4'" get DeviceId


WMI Tasks
@ msdn.microsoft.com


Alle Aliase mit Beschreibung
WMIC /?:FULL


Win32 Provider Classes
@ MSDN.microsoft.com


WMI Switches
@ TechNet.microsoft.com


How To: (Almost) Everything In WMI via C# @ codeproject.com
Part 1 Registry
Part 2 Processes
Part 3 Hardware


WMIC in CommandLine
@www.robvanderwoude.com


WMI C#
@ MSDN.microsoft.com


Read in C#
...

[Mehr ...]
sasa [10:58:40] -

[ Freitag, 21. November ]

IE10 fragt unaufhöhrlich nach PWD (IIS, ASP Seite)

IE10 speichert das einegegebene Passwort nicht und bringt bei Aufruf einer weiteren Unterseite erneut den Authentifizierungsdialog.

Authentifizierung funktioniert aber nach Umstellung des IE Useragent auf IE9 (F12).

Fix: (web.config)
<authentication mode="Forms">
<forms name=".AUTH" cookieless="UseCookies" loginUrl="/" timeout="10000" path="/" />
</authentication>

Weitere Fixes: siehe stackoverflow.com

sasa [08:28:50] -

[ Montag, 06. Oktober ]

Fenster schließen anhand vom Fenstertitel

http://escaron.de/KillWindowByTitle.cs.txt
sasa [08:47:48] -

[ Dienstag, 09. September ]

ScriptCS Plugin für Notepad++

http://csscriptnpp.codeplex.com/
sasa [16:09:15] -

[ Mittwoch, 03. September ]

Typsicheres INotifyPropertyChange

// =================================================== 
// ab .Net 3.5  
// =================================================== 
private void RaisePropertyChanged(Expression<Func<object>> property) 

     var handler = PropertyChanged; 
     if(handler != null) 
     { 
          var expression = property.Body as UnaryExpression; 
          var member = (expression.Operand as MemberExpression).Member; 
          handler(this, new PropertyChangedEventArgs(member.Name)); 
     } 

 
// Aufruf: 
private object _x; 
public object X 

     set 
     { 
          _x = value; 
          RaisePropertyChanged(()=>this.X); 
     } 

 
 
// ===================================================
// Wert abfragen:
Func<Expression<Func<object>>, string> output = (property) =>
{
   var format = "{0}: {1}";
   var expression = ((UnaryExpression) property.Body);
   var prop = ((MemberExpression)(expression.Operand)).Member.Name;
   var value = Expression.Lambda(expression).Compile().DynamicInvoke().ToString();
   return string.Format(format, prop, value);
};
 
 
// =================================================== 
// ab .Net 4.5 
// =================================================== 
protected void RaisePropertyChanged([CallerMemberName] string propertyName = "") 

     var handler = PropertyChanged; 
     if(handler != null && GetType().GetProperty(propertyName) != null) 
          handler(this, new PropertyChangedEventArgs(member.Name)); 

 
// Aufruf: 
private object _y; 
public object Y 

     set 
     { 
          _y = value; 
          RaisePropertyChanged(); 
     }
sasa [15:07:59] -

[ Montag, 25. August ]

Google API styled maps Wizzard

http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html
sasa [16:28:54] -