Skip to content

Commit

Permalink
Add: Object.Try, DirectoryInfo.CopyTo, [TypeInfo].GetSignature
Browse files Browse the repository at this point in the history
ADDED: DirectoryInfo.CopyTo // Copy the source directory to the
destination directory
ADDED: Try // Try an action and return the value or a bool
ADDED: GetSignature // Get the signature from class, method, property,
etc.
FIXED: TypeInfo.GetDeclarations (Added support 'in' modifier, 'out'
modifier, 'new', 'class')
  • Loading branch information
zzzprojects committed Jul 20, 2015
1 parent 237c1ff commit 61188c6
Show file tree
Hide file tree
Showing 92 changed files with 2,741 additions and 32 deletions.
169 changes: 169 additions & 0 deletions src/Z.Core/System.Object/Utility/Object.Try.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System;

public static partial class Extensions
{
/// <summary>A TType extension method that tries.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryFunction">The try function.</param>
/// <returns>A TResult.</returns>
public static TResult Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction)
{
try
{
return tryFunction(@this);
}
catch
{
return default(TResult);
}
}

/// <summary>A TType extension method that tries.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryFunction">The try function.</param>
/// <param name="catchValue">The catch value.</param>
/// <returns>A TResult.</returns>
public static TResult Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, TResult catchValue)
{
try
{
return tryFunction(@this);
}
catch
{
return catchValue;
}
}

/// <summary>A TType extension method that tries.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryFunction">The try function.</param>
/// <param name="catchValueFactory">The catch value factory.</param>
/// <returns>A TResult.</returns>
public static TResult Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, Func<TType, TResult> catchValueFactory)
{
try
{
return tryFunction(@this);
}
catch
{
return catchValueFactory(@this);
}
}

/// <summary>A TType extension method that tries.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryFunction">The try function.</param>
/// <param name="result">[out] The result.</param>
/// <returns>A TResult.</returns>
public static bool Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, out TResult result)
{
try
{
result = tryFunction(@this);
return true;
}
catch
{
result = default(TResult);
return false;
}
}

/// <summary>A TType extension method that tries.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryFunction">The try function.</param>
/// <param name="catchValue">The catch value.</param>
/// <param name="result">[out] The result.</param>
/// <returns>A TResult.</returns>
public static bool Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, TResult catchValue, out TResult result)
{
try
{
result = tryFunction(@this);
return true;
}
catch
{
result = catchValue;
return false;
}
}

/// <summary>A TType extension method that tries.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryFunction">The try function.</param>
/// <param name="catchValueFactory">The catch value factory.</param>
/// <param name="result">[out] The result.</param>
/// <returns>A TResult.</returns>
public static bool Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, Func<TType, TResult> catchValueFactory, out TResult result)
{
try
{
result = tryFunction(@this);
return true;
}
catch
{
result = catchValueFactory(@this);
return false;
}
}

/// <summary>A TType extension method that attempts to action from the given data.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryAction">The try action.</param>
/// <returns>true if it succeeds, false if it fails.</returns>
public static bool Try<TType>(this TType @this, Action<TType> tryAction)
{
try
{
tryAction(@this);
return true;
}
catch
{
return false;
}
}

/// <summary>A TType extension method that attempts to action from the given data.</summary>
/// <typeparam name="TType">Type of the type.</typeparam>
/// <param name="this">The @this to act on.</param>
/// <param name="tryAction">The try action.</param>
/// <param name="catchAction">The catch action.</param>
/// <returns>true if it succeeds, false if it fails.</returns>
public static bool Try<TType>(this TType @this, Action<TType> tryAction, Action<TType> catchAction)
{
try
{
tryAction(@this);
return true;
}
catch
{
catchAction(@this);
return false;
}
}
}
1 change: 1 addition & 0 deletions src/Z.Core/Z.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@
<Compile Include="System.Object\Utility\Object.CoalesceOrDefault.cs" />
<Compile Include="System.Object\Utility\Object.GetValueOrDefault.cs" />
<Compile Include="System.Object\Utility\Object.IfNotNull.cs" />
<Compile Include="System.Object\Utility\Object.Try.cs" />
<Compile Include="System.Object\Utility\Object.NullIf.cs" />
<Compile Include="System.Object\Utility\Object.NullIfEquals.cs" />
<Compile Include="System.Object\Utility\Object.NullIfEqualsAny.cs" />
Expand Down
77 changes: 77 additions & 0 deletions src/Z.IO/System.IO.DirectoryInfo/DirectoryInfo.CopyTo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2015 ZZZ Projects. All rights reserved
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
// Website: http://www.zzzprojects.com/
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System;
using System.IO;

public static partial class Extensions
{
/// <summary>A DirectoryInfo extension method that copies to.</summary>
/// <param name="obj">The obj to act on.</param>
/// <param name="destDirName">Pathname of the destination directory.</param>
public static void CopyTo(this DirectoryInfo obj, string destDirName)
{
obj.CopyTo(destDirName, "*.*", SearchOption.TopDirectoryOnly);
}

/// <summary>A DirectoryInfo extension method that copies to.</summary>
/// <param name="obj">The obj to act on.</param>
/// <param name="destDirName">Pathname of the destination directory.</param>
/// <param name="searchPattern">A pattern specifying the search.</param>
public static void CopyTo(this DirectoryInfo obj, string destDirName, string searchPattern)
{
obj.CopyTo(destDirName, searchPattern, SearchOption.TopDirectoryOnly);
}

/// <summary>A DirectoryInfo extension method that copies to.</summary>
/// <param name="obj">The obj to act on.</param>
/// <param name="destDirName">Pathname of the destination directory.</param>
/// <param name="searchOption">The search option.</param>
public static void CopyTo(this DirectoryInfo obj, string destDirName, SearchOption searchOption)
{
obj.CopyTo(destDirName, "*.*", searchOption);
}

/// <summary>A DirectoryInfo extension method that copies to.</summary>
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
/// <param name="obj">The obj to act on.</param>
/// <param name="destDirName">Pathname of the destination directory.</param>
/// <param name="searchPattern">A pattern specifying the search.</param>
/// <param name="searchOption">The search option.</param>
public static void CopyTo(this DirectoryInfo obj, string destDirName, string searchPattern, SearchOption searchOption)
{
var files = obj.GetFiles(searchPattern, searchOption);
foreach (var file in files)
{
var outputFile = destDirName + file.FullName.Substring(obj.FullName.Length);
var directory = new FileInfo(outputFile).Directory;

if (directory == null)
{
throw new Exception("The directory cannot be null.");
}

if (!directory.Exists)
{
directory.Create();
}

file.CopyTo(outputFile);
}

// Ensure empty dir are also copied
var directories = obj.GetDirectories(searchPattern, searchOption);
foreach (var directory in directories)
{
var outputDirectory = destDirName + directory.FullName.Substring(obj.FullName.Length);
var directoryInfo = new DirectoryInfo(outputDirectory);
if (!directoryInfo.Exists)
{
directoryInfo.Create();
}
}
}
}
1 change: 1 addition & 0 deletions src/Z.IO/Z.IO.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="System.Collections.Generic.IEnumerable[string]\IEnumerable[string].PathCombine.cs" />
<Compile Include="System.Collections.Generic.IEnumerable[DirectoryInfo]\IEnumerable[DirectoryInfo].Delete.cs" />
<Compile Include="System.Collections.Generic.IEnumerable[DirectoryInfo]\IEnumerable[DirectoryInfo].ForEach.cs" />
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.CopyTo.cs" />
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.Clear.cs" />
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.CreateAllDirectories.cs" />
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.DeleteDirectoriesWhere.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static partial class Extensions
public static string GetDeclaraction(this FieldInfo @this)
{
// Example: [Visibility] [Modifier] [Type] [Name] [PostModifier];

var sb = new StringBuilder();

// Variable
Expand Down
65 changes: 50 additions & 15 deletions src/Z.Reflection/GetDeclaration/TypeInfo.GetDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -37,46 +38,80 @@ public static string GetDeclaraction(this Type @this)
// Name
sb.Append(@this.IsGenericType ? @this.Name.Substring(0, @this.Name.IndexOf('`')) : @this.Name);

List<string> constraintType = new List<string>();

// GenericArguments
if (@this.IsGenericType)
{
Type[] arguments = @this.GetGenericArguments();
sb.Append("<");
sb.Append(string.Join(", ", arguments.Select(x =>
{
Type[] constraints = x.GetGenericParameterConstraints();
GenericParameterAttributes sConstraints = x.GenericParameterAttributes;
foreach (Type constraint in constraints)
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.Contravariant))
{
sb.Append("in ");
}
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.Covariant))
{
GenericParameterAttributes gpa = constraint.GenericParameterAttributes;
GenericParameterAttributes variance = gpa & GenericParameterAttributes.VarianceMask;
sb.Append("out ");
}
if (variance != GenericParameterAttributes.None)
{
sb.Append((variance & GenericParameterAttributes.Covariant) != 0 ? "in " : "out ");
}
List<string> parameterConstraint = new List<string>();
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.ReferenceTypeConstraint))
{
parameterConstraint.Add("class");
}
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.DefaultConstructorConstraint))
{
parameterConstraint.Add("new()");
}
if (parameterConstraint.Count > 0)
{
constraintType.Add(x.Name + " : " + string.Join(", " , parameterConstraint));
}
return x.GetShortDeclaraction();
})));
sb.Append(">");

foreach (var argument in arguments)
{
GenericParameterAttributes sConstraints = argument.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
}
}

List<string> constaints = new List<string>();

// Inherited Class
if (@this.BaseType != null && @this.BaseType != typeof (object))
{
hasInheritedClass = true;

sb.Append(" : ");
sb.Append(@this.BaseType.GetShortDeclaraction());
constaints.Add(@this.BaseType.GetShortDeclaraction());
}

// Inherited Interface
Type[] interfaces = @this.GetInterfaces();
if (interfaces.Length > 0)
{
sb.Append(hasInheritedClass ? ", " : " : ");
sb.Append(string.Join(", ", interfaces.Select(x => x.Name)));
constaints.AddRange(interfaces.Select(x => x.Name));
}

if (constaints.Count > 0)
{
sb.Append(" : ");
sb.Append(string.Join(", ", constaints));
}

if (constraintType.Count > 0)
{
sb.Append(" where ");
sb.Append(string.Join(", ", constraintType));
}

return sb.ToString();
Expand Down
Loading

0 comments on commit 61188c6

Please sign in to comment.