aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Updater/MainWindow.xaml
blob: 8733d4e0c181bb0c5553e561c66a9565428d975a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<Window x:Class="Tango.MachineStudio.Updater.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Tango.MachineStudio.Updater"
        mc:Ignorable="d"
        Title="Machine Studio Update" Height="400" Width="700" ShowInTaskbar="False" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="NoResize" Foreground="#202020">

    <Border BorderThickness="1" BorderBrush="#0288D1">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="60"/>
                <RowDefinition Height="156*"/>
            </Grid.RowDefinitions>

            <Grid Background="#0288D1">
                <Grid.Effect>
                    <DropShadowEffect BlurRadius="10" ShadowDepth="5" Direction="270" Opacity="0.5" />
                </Grid.Effect>

                <StackPanel Orientation="Horizontal" Margin="8" HorizontalAlignment="Center">
                    <Image Source="/Images/machine-trans.png" RenderOptions.BitmapScalingMode="Fant"></Image>
                    <TextBlock Foreground="White" VerticalAlignment="Center" Margin="10 0 0 0" FontSize="23">MACHINE STUDIO</TextBlock>
                </StackPanel>
            </Grid>

            <Grid Grid.Row="1">
                <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Image Source="/Images/update.png" Width="100" />
                    <TextBlock x:Name="txtStatus" Margin="0 20 0 0" FontSize="12" Text="Updating Machine Studio..." HorizontalAlignment="Center"></TextBlock>
                    <ProgressBar x:Name="prog" Margin="0 30 0 0" Width="500" Height="10" Maximum="100" Value="0" Foreground="#0288D1"></ProgressBar>
                </StackPanel>
            </Grid>
        </Grid>
    </Border>
</Window>
me.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// Contains <see cref="Type"/> extension methods.
/// </summary>
public static class TypeExtensions
{
    private class AssemblyExtensionMethods
    {
        public Assembly Assembly { get; set; }
        public List<MethodInfo> Methods { get; set; }

        public AssemblyExtensionMethods()
        {
            Methods = new List<MethodInfo>();
        }
    }

    private static List<AssemblyExtensionMethods> _extensionMethodsCache = new List<AssemblyExtensionMethods>();

    /// <summary>
    /// Gets all the extension methods registered in the specified assembly.
    /// </summary>
    /// <param name="type">The type.</param>
    /// <param name="extensionsAssembly">The extensions assembly.</param>
    /// <returns></returns>
    public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, Assembly extensionsAssembly)
    {
        if (type.Name == "CalculateRequest")
        {

        }

        var asmMethods = _extensionMethodsCache.FirstOrDefault(x => x.Assembly == extensionsAssembly);

        if (asmMethods == null)
        {
            var query = from t in extensionsAssembly.GetTypes()
                        where !t.IsGenericType && !t.IsNested
                        from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                        where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)
                        select m;

            asmMethods = new AssemblyExtensionMethods();
            asmMethods.Assembly = extensionsAssembly;
            asmMethods.Methods = query.ToList();
            _extensionMethodsCache.Add(asmMethods);
        }

        List<MethodInfo> methods = new List<MethodInfo>();

        asmMethods.Methods.Where(x => x.GetParameters()[0].ParameterType.IsAssignableFrom(type));

        foreach (var method in asmMethods.Methods)
        {
            var parameter = method.GetParameters()[0];

            if (parameter.ParameterType.IsGenericParameter)
            {
                var constraints = parameter.ParameterType.GetGenericParameterConstraints().ToList();

                if (constraints.Count > 0)
                {
                    if (constraints[0].IsAssignableFrom(type))
                    {
                        methods.Add(method);
                    }
                    else if (constraints[0].GetInterfaces().Any(x => x.IsAssignableFrom(type)))
                    {
                        methods.Add(method);
                    }
                }
            }
            else
            {
                if (parameter.ParameterType.IsAssignableFrom(type))
                {
                    methods.Add(method);
                }
            }
        }

        return methods;
    }

    /// <summary>
    /// Gets the specified extension method by assembly and name.
    /// </summary>
    /// <param name="type">The type.</param>
    /// <param name="extensionsAssembly">The extensions assembly.</param>
    /// <param name="name">The name.</param>
    /// <returns></returns>
    public static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name)
    {
        return type.GetExtensionMethods(extensionsAssembly).FirstOrDefault(m => m.Name == name);
    }

    /// <summary>
    /// Gets the extension method.
    /// </summary>
    /// <param name="type">The type.</param>
    /// <param name="extensionsAssembly">The extensions assembly.</param>
    /// <param name="name">The name.</param>
    /// <param name="types">The types.</param>
    /// <returns></returns>
    private static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name, Type[] types)
    {
        var methods = (from m in type.GetExtensionMethods(extensionsAssembly)
                       where m.Name == name
                       && m.GetParameters().Count() == types.Length + 1 // + 1 because extension method parameter (this)
                       select m).ToList();

        if (!methods.Any())
        {
            return default(MethodInfo);
        }

        if (methods.Count() == 1)
        {
            return methods.First();
        }

        foreach (var methodInfo in methods)
        {
            var parameters = methodInfo.GetParameters();

            bool found = true;
            for (byte b = 0; b < types.Length; b++)
            {
                found = true;
                if (parameters[b].GetType() != types[b])
                {
                    found = false;
                }
            }

            if (found)
            {
                return methodInfo;
            }
        }

        return default(MethodInfo);
    }

    /// <summary>
    /// Gets the type properties that are decorated with the specified attribute type T.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="type">The type.</param>
    /// <param name="bindingFlags">Binding flags.</param>
    /// <returns></returns>
    public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<T>(this Type type, BindingFlags bindingFlags) where T : Attribute
    {
        return type.GetProperties(bindingFlags).Where(x => x.GetCustomAttribute<T>() != null);
    }

    /// <summary>
    /// Gets the type fields that are decorated with the specified attribute type T.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="type">The type.</param>
    /// <param name="bindingFlags">Binding flags.</param>
    /// <returns></returns>
    public static IEnumerable<FieldInfo> GetFieldsWithAttribute<T>(this Type type, BindingFlags bindingFlags) where T : Attribute
    {
        return type.GetFields(bindingFlags).Where(x => x.GetCustomAttribute<T>() != null);
    }

    /// <summary>
    /// Gets the type properties that are decorated with the specified attribute type T.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="type">The type.</param>
    /// <returns></returns>
    public static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<T>(this Type type) where T : Attribute
    {
        return type.GetProperties().Where(x => x.GetCustomAttribute<T>() != null);
    }

    public static bool IsGenericTypeAndNotNullable(this Type type)
    {
        return type.IsGenericType && Nullable.GetUnderlyingType(type) == null;
    }

    public static bool IsNullable(this Type type)
    {
        return Nullable.GetUnderlyingType(type) != null;
    }

    public static bool IsValueTypeOrString(this Type type)
    {
        return type.IsValueType || type == typeof(String);
    }
}