-
-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Object.Try, DirectoryInfo.CopyTo, [TypeInfo].GetSignature
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
Showing
92 changed files
with
2,741 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.