Website may be up and down over next few months. I'm currently doing a complete overhaul of everything. Going back to simple individual .htm pages, new overall site theme, sanitizing and cleaning up html of all pages and blog posts, attempting to implement a new tooling and publishing system etc etc.

The XAML. Use null to define a separator.

<Menu Name="MainMenu" Grid.Row="0" IsMainMenu="True" ItemsSource="{Binding Model}">
    <Menu.Resources>
        <ControlTemplate x:Key="MenuSeparatorTemplate">
            <Separator />
        </ControlTemplate>
    </Menu.Resources>
    <Menu.ItemContainerStyle>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Command" Value="{Binding Command}" />
            <Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
            <Setter Property="Header" Value="{Binding Header}" />
            <Setter Property="ItemsSource" Value="{Binding Children}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding }" Value="{x:Null}">
                    <Setter Property="Template" Value="{StaticResource MenuSeparatorTemplate}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Menu.ItemContainerStyle>
</Menu>

Below is a post I made on stack overflow.

--------------------------

Stack Overflow. We meet again.  /Sigh

Is it just me or is anybody else getting wacky messages from Directory.CreateDirectory.

Is it just me or do other people have code that has been running flawlessly for years only to suddenly go poof at random for no reason even, when that code has not been changed at all?

Running with windows 10, VS2019. All projects are .NET Core 3.1 or .NET Std 2.0/2.1.

Anyway, I have this code ISettingsProvider that gets implemented by my class for managing app settings. (Stored in xml format) In that class constructor I pass in a string representing the file path to the xml file were the settings will be stored. If the file does not already exist I grab the folder path ala Path.GetDirectoryName and check if it exists and if it does not create it, then write out a empty setting file to that folder. Other wise do nothing because we are about to read the data from the file immediately afterwards.

Below is the constructor for that class. Again this code has been working flawlessly for me for years, across multiple platforms versions and projects, .net pcl 3.5 4.6 .NET Std, Unity3D etc etc

    /// <summary>
    /// Initializes a new instance of the <see cref="XmlSettingsProvider"/> class.
    /// </summary>
    /// <param name="fileName">
    /// The file name.
    /// </param>
    /// <param name="create"><c>true</c> Create the settings file immediately if one does not exist.</param>
    /// <exception cref="ArgumentNullException">
    /// If <see cref="fileName"/> is null or empty.
    /// </exception>
    public XmlSettingsProvider(string fileName, bool create)
    {
        if (string.IsNullOrEmpty(fileName))
        {
            throw new ArgumentNullException(nameof(fileName));
        }

        var directoryName = Path.GetDirectoryName(fileName);
        if (directoryName != null && directoryName.IndexOfAny(Path.GetInvalidPathChars()) != -1)
        {
            throw new Exception("Invalid path characters detected!");
        }

        var name = Path.GetFileName(fileName);
        if (name != null && name.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
        {
            throw new Exception("Invalid filename characters detected!");
        }

        this.readDelayInSeconds = 5;
        this.FileName = fileName;

        if (create && !File.Exists(fileName))
        {
            var doc = new XmlDocument();
            var declaration = doc.CreateXmlDeclaration("1.0", null, null);

            var settings = doc.CreateElement("settings");
            doc.AppendChild(settings);
            doc.InsertBefore(declaration, doc.DocumentElement);

            Directory.CreateDirectory(directoryName);
            using (var stream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                doc.Save(stream);
            }
        }

        this.Read();
    }

The problem is with Directory.CreateDirectory. I am trying to create a directory at "d:\documents\StockWatchWpfCore" where "d:\documents" exists and is my windows 10 "My Documents" folder and the "d:\documents\StockWatchWpfCore" does not yet exist.

I thought maybe it was windows 10 Ransomware detection preventing my app from creating a folder in "d:\documents" because I have "d:\documents" added as a protected folder. But I am not getting any messages in Block History stating my app is being blocked.

To make matters worse, or more confusing, the exception info that is being raised is not what you would expect.

The exception data being thrown.

Call Stack

at System.IO.FileSystem.CreateDirectory(String fullPath, Byte[] securityDescriptor)
at System.IO.Directory.CreateDirectory(String path)
at Codefarts.AppCore.SettingProviders.Xml.XmlSettingsProvider..ctor(String fileName, Boolean create) in P:\Code Projects\Codefarts.AppCore\Setting Providers\Codefarts.AppCore.SettingProviders.Xml\XmlSettingsProvider.cs:line 142
at Codefarts.AppCore.SettingProviders.Xml.XmlSettingsProvider..ctor(String fileName) in P:\Code Projects\Codefarts.AppCore\Setting Providers\Codefarts.AppCore.SettingProviders.Xml\XmlSettingsProvider.cs:line 162
at Codefarts.WpfAppBootstrapper.BootstrappedApp.OnStartup(StartupEventArgs e) in P:\Code Projects\Codefarts.WpfAppBootstrapper\BootstrappedApp.cs:line 63
at System.Windows.Application.<.ctor>b__1_0(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)

Source

"System.IO.FileSystem"

Message

"Could not find file 'd:\\Documents\\StockWatchWpfCore'."

InnerException

null

Did you notice the error message? "Could not find file 'd:\\Documents\\StockWatchWpfCore'."  File? I'm working with directories not files?!?!

The plot thickens ......

So in another part of my code I have hooked into the AppDomain.ResolveAssemblies event. Interestingly as soon as I step into Directory.CreateDirectory the ResolveAssemblies event gets fired 2 times both with the same request

Name

    "System.IO.FileSystem.resources, Version=4.1.2.0, Culture=en-US, PublicKeyToken=b03f5f7f11d50a3a"

RequestingAssembly

    {System.IO.FileSystem, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a}

My assembly resolve event code

private Assembly ResolveAssemblies(object sender, System.ResolveEventArgs args)
    {
            var folderPaths = new List<string>(this.AssemblySearchFolders);
            folderPaths.Add(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
            var filter = new AssemblyName(args.Name);

            foreach (var folderPath in folderPaths)
            {
                if (!Directory.Exists(folderPath))
                {
                    continue;
                }

                var fileMatches = Directory.GetFiles(folderPath, filter.Name + ".dll", SearchOption.AllDirectories);
                var assemblyPath = fileMatches.FirstOrDefault();
                if (!string.IsNullOrWhiteSpace(assemblyPath) && File.Exists(assemblyPath))
                {
                    return Assembly.LoadFrom(assemblyPath);
                }
            }

        return null;
    }

BTW "this.AssemblySearchFolders" is an empty collection. It's just there in case I want to search other locations other then the app install folder.

Ok so at this point it seems like .net is trying to resolve a missing assembly reference that it can't find on it's own. So I opened up my project containing the XmlSettingsProvider implementation. That project is a .NET Core 3.1 project type with this project info

<Project Sdk="Microsoft.NET.Sdk">
    
        <PropertyGroup>
            <TargetFramework>netcoreapp3.1</TargetFramework>
            <AssemblyVersion>2020.5.10.9</AssemblyVersion>
            <FileVersion>2020.5.10.9</FileVersion>
        </PropertyGroup>
    
        <PropertyGroup>
            <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
        </PropertyGroup>       
    
        <ItemGroup>
            <ProjectReference Include="..\..\Codefarts.AppCoreNETStd\Codefarts.AppCoreNETStd2.csproj" />
        </ItemGroup> 
    </Project>

Notice that after I started having this issue I made sure to include the CopyLocalLockFileAssemblies property in my .csproj file. But it does not copy any references to the bin folder like "System.IO.FileSystem".

Ok, so then I tried to do a nuget search for System.IO.FileSystem and found this https://www.nuget.org/packages/System.IO.FileSystem/4.3.0?_src=template

It appears to support .NET Standard 1.3 but no mention of .NET Core. I added the nuget reference anyway and did a rebuild then ran my app again and still the same issue. No System.IO.FileSystem.dll being copied to the bin folder and AssemblyResolve event still being fired still looking for missing assembly references.

It's at this point I'm running out of ideas and am at the limit of my understandings. Also the googles is not helping. It's frustrating because the XmlSettingsProvider code has not changed and has been working fine for the last two weeks.. All of a sudden it decides to pipe up and start giving my this error last two days.

As a simplified test I started a new .Net Core 3.1 console app with this code and I get the same issue with not being able to resolve System.IO.FileSystem

using System;
    using System.IO;
    
    namespace assembly_resolve
    {
        class Program
        {
            static void Main(string[] args)
            {
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
                Directory.CreateDirectory(@"d:\documents\wpfcore");
            }
    
            private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                return args.RequestingAssembly;
            }
        }
    }

Also as a test of my sanity I wrote this linqPad6 test code.

void Main()
    {
        AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
        {        
            return e.RequestingAssembly;
        };
    
        Directory.CreateDirectory(@"d:\documents\WpfCoreTest");
    }

The test code does not fire the AssemblyResolve event as I new it would not. Also tried obligatory system shutdown and reboot to no avail.  So it would seem that there are architecture/behaviour changes moving from .NET Framework to .NET Core. Ala -> https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.assemblyresolve?view=netcore-3.1

Also tried a hail marry via this (Shameless plug) http://www.createdbyx.com/createdbyx/post/2016/07/02/MSBuild-Recursively-Copy-Indirect-Project-Dependencies-to-bin.aspx  But still no success.

So in conclusion I need to somehow instruct .net where to find the assembly "System.IO.FileSystem" digery do so it can "Do the thing"

Pweaze Halp!

==========================

UPDATE: The next day. Was up thinking about it all night. My whole mindset was toward something going wrong with .NET Core, or some kind of user permissions etc so I created additional console projects in C# and VS2019 targeting .NET Framework 4 Client Profile, 4.6, 4.7, .NET Core 3.1 all of them did not work and throw the same error when trying to create a directory. Now I know they have worked in the past but could not figure out why it has stopped working. I also tried running vs2019 in administrator mode to no avail. What is going on?

I noticed the version number of the assembly it was trying to look for "System.IO.FileSystem.resources, Version=4.1.2.0, ..." When I look at the [nuget package][1] that package has no version 4.1.2.0. Additionally when I look in my "C:\Users\Dean\.nuget\packages\system.io.filesystem" folder there is no corresponding folder for 4.1.2.

So my thinking now is that something has gone horribly wrong on my system and is not my code at all.

Then I tried turning off windows ransomware protection and .... what the hell ... it started working. Which makes things more interesting because of the fact that there is no block history reports in windows ransomware protection indicating that it is protecting my system from my own application code?!!?! But I get block history notifications and reports every other day from other applications on my system. As a side note I do have vs2019 devenv.exe added as an allowed app.

Added my application .exe to "Add an app through Controlled folder access" and voila it's working. Also modified the application code and re ran it to check if ransomware protection would detect a different version of my app and it did not. So that suggests it's not doing any type of crc/file contents comparison check from when the block rule was first created.

I'm wondering if it's because it's a .net application that ransomware protection does not report block history. In any case the issue has been tracked down, my sanity has been restored, and I am in the process of filing a bug report via the "Give us feedback" link located on the right of the ransomware protection window.


New helpful utilities list

Published 4/4/2020 by createdbyx in News
Tags:

Added a new page containing a list of helpfull utilities.


/// <summary>
/// Determines whether an enum has been marked Obsolete.
/// </summary>
/// <param name="value">The enum value to check against.</param>
/// <returns>
///   <c>true</c> if marked obsolete; otherwise, <c>false</c>.
/// </returns>
public static bool IsEnumObsolete(this Enum value)
{
    var fi = value.GetType().GetField(value.ToString());
    var attributes = (ObsoleteAttribute[])fi.GetCustomAttributes(typeof(ObsoleteAttribute), false);
    return attributes.Length > 0;
}


 

/// <summary>Removes any items in a list that match a criteria</summary>
/// <param name="list">The list.</param>
/// <param name="predicate">The callback used to determine weather or not the item in the list should be removed.</param>
/// <exception cref="ArgumentNullException"><paramref name="predicate"/> is <see langword="null"/></exception>
public static void RemoveAny(this IList list,  Predicate<object> predicate )
{
    if (predicate == null)
    {
        throw new ArgumentNullException("predicate");
    }

    for (var i = list.Count - 1; i >= 0; i--)
    {
        var item = list[i];
        if (predicate(item))
        {
            list.RemoveAt(i);
        }
    }
}

I was recently working on a WPF MVVM application that used MEF to load plugins. I had broken up the main WPF application into smaller more manageable compartmentalized projects to help ensure more cross platform code. But when I tried to run the program it would start up fine but as soon as it tried to utilize logic from my plugins I was getting "assembly or one of it’s dependencies not found" exceptions.

Main Application
    -> LibraryA (References to Xceed.WPF.Toolkit)

Turns out when visual studio compiled my solution it was not copying Xceed.Wpf.Tookit into the main application bin/Debug folder. To solve this took a lot of searching on the interwebz before coming across this handy solution. Recursively Copying Indirect Project Dependencies in MSBuild.

Just download the CopyIndirectDependencies.targets CopyIndirectDependencies.targets (9.19 kb) and put it in your main application folder. Then edit your projects .csproj file to include this line "<Import Project="CopyIndirectDependencies.targets" />"

 

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  ...

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="CopyIndirectDependencies.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

Now when you build your main application project it should scan for indirect dependancies and copy them to the projects bin/Debug folder.


Provided below are two extension methods for determining if two unity Rect types intersect one another.

/// <summary>
/// Returns a third Rect structure that represents the intersection of two other Rect structures. 
/// If there is no intersection, an empty Rect is returned.
/// </summary>
/// <param name="a">
/// A rectangle to intersect.   
/// </param>
/// <param name="b">
/// B rectangle to intersect.  
/// </param>
/// <returns>
/// A Rect that represents the intersection of a and b.
/// </returns>
public static Rect Intersect(this Rect a, Rect b)
{
    float x = Math.Max((sbyte)a.x, (sbyte)b.x);
    var num2 = Math.Min(a.x + a.width, b.x + b.width);
    float y = Math.Max((sbyte)a.y, (sbyte)b.y);
    var num4 = Math.Min(a.y + a.height, b.y + b.height);
    if ((num2 >= x) && (num4 >= y))
    {
        return new Rect(x, y, num2 - x, num4 - y);
    }

    return new Rect();
}

/// <summary>
/// Determines if this rectangle intersects with rect.
/// </summary>
/// <param name="source">The source rectangle from which the intersection will be tested.</param>
/// <param name="rect">
/// The rectangle to test.
/// </param>
/// <returns>
/// This method returns true if there is any intersection, otherwise false.
/// </returns>
public static bool Intersects(this Rect source, Rect rect)
{
    return !((source.x > rect.xMax) || (source.xMax< rect.x) || (source.y > rect.yMax) || (source.yMax < rect.y));
}

The DrawRectangle & DrawLine helper methods allow you to draw a lines and rectangles inside OnGUI methods.

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.</param>
public static void DrawRectangle(Rect rect)
{
    DrawRectangle(rect, GUI.contentColor, 1f);
}

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">
/// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
/// </param>
/// <param name="color">
/// The color of the rectangle.
/// </param>
public static void DrawRectangle(Rect rect, Color color)
{
    DrawRectangle(rect, color, 1);
}

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">
/// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
/// </param>
/// <param name="thickness">
/// The line thickness of the rectangle.
/// </param>
public static void DrawRectangle(Rect rect, float thickness)
{
    DrawRectangle(rect, GUI.contentColor, thickness);
}

/// <summary>
/// Draws a rectangle.
/// </summary>
/// <param name="rect">
/// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
/// </param>
/// <param name="color">
/// The color of the rectangle.
/// </param>
/// <param name="thickness">
/// The line thickness of the rectangle.
/// </param>
public static void DrawRectangle(Rect rect, Color color, float thickness)
{
    DrawHLine(new Vector2(rect.x, rect.y), rect.width, color, thickness);
    DrawHLine(new Vector2(rect.x, rect.y + rect.height - 1), rect.width, color, thickness);
    DrawVLine(new Vector2(rect.x, rect.y), rect.height, color, thickness);
    DrawVLine(new Vector2(rect.x + rect.width - 1, rect.y), rect.height, color, thickness);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB)
{
    DrawLine(pointA, pointB, GUI.contentColor, 1.0f);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
/// <param name="color">The color to use.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color)
{
    DrawLine(pointA, pointB, color, 1.0f);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
/// <param name="thickness">THe thickness of the line.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB, float thickness)
{
    DrawLine(pointA, pointB, GUI.contentColor, thickness);
}

/// <summary>
/// Draws a line.
/// </summary>
/// <param name="pointA">The start point of the line.</param>
/// <param name="pointB">The end point of the line.</param>
/// <param name="color">The color to use.</param>
/// <param name="thickness">THe thickness of the line.</param>
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float thickness)
{
    // Save the current GUI matrix, since we're going to make changes to it.
    var matrix = GUI.matrix;

    // Store current GUI color, so we can switch it back later,
    // and set the GUI color to the color parameter
    var savedColor = GUI.color;
    GUI.color = color;

    // Determine the angle of the line.
    var angle = Vector2.Angle(pointB - pointA, Vector2.right);

    // Vector3.Angle always returns a positive number.
    // If pointB is above pointA, then angle needs to be negative.
    if (pointA.y > pointB.y)
    {
        angle = -angle;
    }

    // Use ScaleAroundPivot to adjust the size of the line.
    // We could do this when we draw the texture, but by scaling it here we can use
    //  non-integer values for the thickness and length (such as sub 1 pixel widths).
    // Note that the pivot point is at +.5 from pointA.y, this is so that the thickness of the line
    //  is centered on the origin at pointA.
    GUIUtility.ScaleAroundPivot(new Vector2((pointB - pointA).magnitude, thickness), new Vector2(pointA.x, pointA.y + 0.5f));

    // Set the rotation for the line.
    //  The angle was calculated with pointA as the origin.
    GUIUtility.RotateAroundPivot(angle, pointA);

    // Finally, draw the actual line.
    // We're really only drawing a 1x1 texture from pointA.
    // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
    //  render with the proper thickness, length, and angle.
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, 1, 1), Texture2D.whiteTexture);

    // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
    GUI.matrix = matrix;
    GUI.color = savedColor;
}

/// <summary>
/// Draws a horizontal line.
/// </summary>
/// <param name="pointA">
/// The start point of the line.
/// </param>
/// <param name="length">
/// The length of the line.
/// </param>
/// <param name="color">
/// The color to use.
/// </param>
/// <param name="thickness">
/// THe thickness of the line.
/// </param>
/// <remarks>
/// The line will be drawn from <see cref="pointA"/> moving to the right. To draw to the left use a negative <see cref="length"/> value.
/// </remarks>
public static void DrawHLine(Vector2 pointA, float length, Color color, float thickness)
{
    // Store current GUI color, so we can switch it back later,
    // and set the GUI color to the color parameter
    var savedColor = GUI.color;
    GUI.color = color;

    // Finally, draw the actual line.
    // We're really only drawing a 1x1 texture from pointA.
    // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
    //  render with the proper thickness, length, and angle.
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, length - 1, thickness), Texture2D.whiteTexture, ScaleMode.StretchToFill);

    // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
    GUI.color = savedColor;
}

/// <summary>
/// Draws a vertical line.
/// </summary>
/// <param name="pointA">
/// The start point of the line.
/// </param>
/// <param name="length">
/// The length of the line.
/// </param>
/// <param name="color">
/// The color to use.
/// </param>
/// <param name="thickness">
/// THe thickness of the line.
/// </param>
/// <remarks>
/// The line will be drawn from <see cref="pointA"/> moving up. To draw down use a negative <see cref="length"/> value.
/// </remarks>
public static void DrawVLine(Vector2 pointA, float length, Color color, float thickness)
{
    // Store current GUI color, so we can switch it back later,
    // and set the GUI color to the color parameter
    var savedColor = GUI.color;
    GUI.color = color;

    // Finally, draw the actual line.
    // We're really only drawing a 1x1 texture from pointA.
    // The matrix operations done with ScaleAroundPivot and RotateAroundPivot will make this
    //  render with the proper thickness, length, and angle.
    GUI.DrawTexture(new Rect(pointA.x, pointA.y, thickness, length - 1), Texture2D.whiteTexture, ScaleMode.StretchToFill);

    // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
    GUI.color = savedColor;
}

The FillRectangle helper methods allow you to draw a filled rectangles inside OnGUI methods.

        /// <summary>
        /// Draws a filled rectangle.
        /// </summary>
        /// <param name="rect">
        /// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
        /// </param>
        public static void FillRectangle(Rect rect)
        {
            FillRectangle(rect, GUI.color);
        }

        /// <summary>
        /// Draws a filled rectangle.
        /// </summary>
        /// <param name="rect">
        /// A <see cref="Rect"/> type that defines the bounds of the rectangle to draw.
        /// </param>
        /// <param name="color">
        /// The color of the rectangle.
        /// </param>
        public static void FillRectangle(Rect rect, Color color)
        {
            // Store current GUI color, so we can switch it back later,
            // and set the GUI color to the color parameter
            var savedColor = GUI.color;
            GUI.color = color;

            // Finally, draw the actual rectangle.
            GUI.DrawTexture(rect, Texture2D.whiteTexture, ScaleMode.StretchToFill);

            // We're done.  Restore the GUI matrix and GUI color to whatever they were before.
            GUI.color = savedColor;
        }

Some times you need to make sure a component has been set on a game object and if it does not exist add it.

        /// <summary>
        /// Tries to get a component and if it does not exist adds the component and returns a reference to it. 
        /// </summary>
        /// <remarks>
        /// <example>
        /// Usage example:
        /// <code>
        /// var boxCollider = transform.GetOrAddComponent<BoxCollider>();
        /// </code>
        /// </example>
        /// </remarks>
        public static T GetOrAddComponent<T>(this Component child) where T : Component
        {
            var result = child.GetComponent<T>();
            if (result == null)
            {
                result = child.gameObject.AddComponent<T>();
            }

            return result;
        }

Created by: X

Just another personal website in this crazy online world

Name of author Dean Lunz (aka Created by: X)
Computer programming nerd, and tech geek.
About Me -- Resume