Posts mit dem Label C# werden angezeigt. Alle Posts anzeigen
Posts mit dem Label C# werden angezeigt. Alle Posts anzeigen

1.24.2018

c# Liste generischer Klassen mit unterschiedlichen Typen

Ziel: Eine Liste von generischen Klassen (class) mit unterschiedlichen Typen (also unterschiedlichen T).

Vorarbeiten:
Nicht generische abstrakte BaseClass und generische Ableitung:
    public abstract class ReportFilter
    {
        public string Description { get; set; }
        public Type Type { get; set; }
    }

    public class ReportFilter : ReportFilter
    {
        public T Value { get; set; }
    }
 
Liste mit zwei Einträgen:
 var reportFilters = new List()
            {
                new ReportFilter<string> {Description="Name",Type=typeof(string),Value="Test"},
                new ReportFilter<bool> {Description="IsAktiv",Type=typeof(bool),Value=true}
            };
Auslesen:
 foreach (var reportFilter in reportFilters)
            {
                var type = typeof(ReportFilter<>).MakeGenericType(reportFilter.Type);
                var aktFilter = Convert.ChangeType(reportFilter, type);
                var propertyInfo = type.GetProperty("Value");
                if (propertyInfo != null)
                {
                    var val = propertyInfo.GetValue(aktFilter, null);
                }
            }
 
Kopierbare Version der Code-Schnipsel:
public abstract class ReportFilter { public string Description { get; set; } public Type Type { get; set; } } public class ReportFilter : ReportFilter { public T Value { get; set; } }

var reportFilters = new List() { new ReportFilter {Description="Name",Type=typeof(string),Value="Test"}, new ReportFilter {Description="IsAktiv",Type=typeof(bool),Value=true} }; foreach (var reportFilter in reportFilters) { var type = typeof(ReportFilter<>).MakeGenericType(reportFilter.Type); var aktFilter = Convert.ChangeType(reportFilter, type); var propertyInfo = type.GetProperty("Value"); if (propertyInfo != null) { var val = propertyInfo.GetValue(aktFilter, null); } }

4.21.2016

IP-Adresse des Client in einem Web.API 2.x Controler ermitteln

Nach dem Einbinden von System.ServiceModel funktioniert folgender Code:


private string GetClientIp(HttpRequestMessage request = null)
       {
           request = request ?? this.Request;

           if (request.Properties.ContainsKey("MS_HttpContext"))
           {
               return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
           }
           else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
           {
               RemoteEndpointMessageProperty prop = (RemoteEndpointMessageProperty)this.Request.Properties[RemoteEndpointMessageProperty.Name];
               return prop.Address;
           }
           else if (HttpContext.Current != null)
           {
               return HttpContext.Current.Request.UserHostAddress;
           }
           else
           {
               return null;
           }
       }

1.14.2016

Events als Behaviors / Events in MVVM

Für WPF:
Verweis auf Microsoft.XAML.Interactions notwendig
...
xmlns:i=http://schemas.microsoft.com/expression/2010/interactivity
...
<i:Interaction.Triggers>


<i:EventTrigger EventName="Loaded">


<i:InvokeCommandAction Command="{Binding LoadedCommand}" />


</i:EventTrigger>


</i:Interaction.Triggers>


Für WinPhone 8.1:
Verweis auf Behaviors SDK (XAML)
...
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"


xmlns:core="using:Microsoft.Xaml.Interactions.Core"
...
<interactivity:Interaction.Behaviors>


<core:EventTriggerBehavior EventName="Tapped">


<core:InvokeCommandAction Command="{Binding RufeNummerAnCommand, Mode=OneWay}"/>


</core:EventTriggerBehavior>


</interactivity:Interaction.Behaviors>

11.11.2015

Singelton in C#

#region Singleton
       private static xxx _instance;

       private xxx() { }

       public static xxx Instance
       {
           get
           {
               if (_instance == null)
               {
                   _instance = new xxx();
               }
               return _instance;
           }
       }
       #endregion

c#-Code zu lesbarem HTML

http://csharpindepth.com/CodeFormatterTool.aspx

Converter, die man (fast) immer braucht

Sichtbarkeit über Bool (unsichtbar):

public class Boolean2VisibleConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool)
        {
            if ((bool)value)
                return Visibility.Visible;
        }
        return Visibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Sichtbarkeit über Bool (ausgeblendet):

public class Boolean2VisibleCollapsedConverter:IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool)
            {
                if ((bool)value)
                    return Visibility.Visible;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }