Skip to content

Commit 61188c6

Browse files
author
zzzprojects
committed
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')
1 parent 237c1ff commit 61188c6

File tree

92 files changed

+2741
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+2741
-32
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System;
8+
9+
public static partial class Extensions
10+
{
11+
/// <summary>A TType extension method that tries.</summary>
12+
/// <typeparam name="TType">Type of the type.</typeparam>
13+
/// <typeparam name="TResult">Type of the result.</typeparam>
14+
/// <param name="this">The @this to act on.</param>
15+
/// <param name="tryFunction">The try function.</param>
16+
/// <returns>A TResult.</returns>
17+
public static TResult Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction)
18+
{
19+
try
20+
{
21+
return tryFunction(@this);
22+
}
23+
catch
24+
{
25+
return default(TResult);
26+
}
27+
}
28+
29+
/// <summary>A TType extension method that tries.</summary>
30+
/// <typeparam name="TType">Type of the type.</typeparam>
31+
/// <typeparam name="TResult">Type of the result.</typeparam>
32+
/// <param name="this">The @this to act on.</param>
33+
/// <param name="tryFunction">The try function.</param>
34+
/// <param name="catchValue">The catch value.</param>
35+
/// <returns>A TResult.</returns>
36+
public static TResult Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, TResult catchValue)
37+
{
38+
try
39+
{
40+
return tryFunction(@this);
41+
}
42+
catch
43+
{
44+
return catchValue;
45+
}
46+
}
47+
48+
/// <summary>A TType extension method that tries.</summary>
49+
/// <typeparam name="TType">Type of the type.</typeparam>
50+
/// <typeparam name="TResult">Type of the result.</typeparam>
51+
/// <param name="this">The @this to act on.</param>
52+
/// <param name="tryFunction">The try function.</param>
53+
/// <param name="catchValueFactory">The catch value factory.</param>
54+
/// <returns>A TResult.</returns>
55+
public static TResult Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, Func<TType, TResult> catchValueFactory)
56+
{
57+
try
58+
{
59+
return tryFunction(@this);
60+
}
61+
catch
62+
{
63+
return catchValueFactory(@this);
64+
}
65+
}
66+
67+
/// <summary>A TType extension method that tries.</summary>
68+
/// <typeparam name="TType">Type of the type.</typeparam>
69+
/// <typeparam name="TResult">Type of the result.</typeparam>
70+
/// <param name="this">The @this to act on.</param>
71+
/// <param name="tryFunction">The try function.</param>
72+
/// <param name="result">[out] The result.</param>
73+
/// <returns>A TResult.</returns>
74+
public static bool Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, out TResult result)
75+
{
76+
try
77+
{
78+
result = tryFunction(@this);
79+
return true;
80+
}
81+
catch
82+
{
83+
result = default(TResult);
84+
return false;
85+
}
86+
}
87+
88+
/// <summary>A TType extension method that tries.</summary>
89+
/// <typeparam name="TType">Type of the type.</typeparam>
90+
/// <typeparam name="TResult">Type of the result.</typeparam>
91+
/// <param name="this">The @this to act on.</param>
92+
/// <param name="tryFunction">The try function.</param>
93+
/// <param name="catchValue">The catch value.</param>
94+
/// <param name="result">[out] The result.</param>
95+
/// <returns>A TResult.</returns>
96+
public static bool Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, TResult catchValue, out TResult result)
97+
{
98+
try
99+
{
100+
result = tryFunction(@this);
101+
return true;
102+
}
103+
catch
104+
{
105+
result = catchValue;
106+
return false;
107+
}
108+
}
109+
110+
/// <summary>A TType extension method that tries.</summary>
111+
/// <typeparam name="TType">Type of the type.</typeparam>
112+
/// <typeparam name="TResult">Type of the result.</typeparam>
113+
/// <param name="this">The @this to act on.</param>
114+
/// <param name="tryFunction">The try function.</param>
115+
/// <param name="catchValueFactory">The catch value factory.</param>
116+
/// <param name="result">[out] The result.</param>
117+
/// <returns>A TResult.</returns>
118+
public static bool Try<TType, TResult>(this TType @this, Func<TType, TResult> tryFunction, Func<TType, TResult> catchValueFactory, out TResult result)
119+
{
120+
try
121+
{
122+
result = tryFunction(@this);
123+
return true;
124+
}
125+
catch
126+
{
127+
result = catchValueFactory(@this);
128+
return false;
129+
}
130+
}
131+
132+
/// <summary>A TType extension method that attempts to action from the given data.</summary>
133+
/// <typeparam name="TType">Type of the type.</typeparam>
134+
/// <param name="this">The @this to act on.</param>
135+
/// <param name="tryAction">The try action.</param>
136+
/// <returns>true if it succeeds, false if it fails.</returns>
137+
public static bool Try<TType>(this TType @this, Action<TType> tryAction)
138+
{
139+
try
140+
{
141+
tryAction(@this);
142+
return true;
143+
}
144+
catch
145+
{
146+
return false;
147+
}
148+
}
149+
150+
/// <summary>A TType extension method that attempts to action from the given data.</summary>
151+
/// <typeparam name="TType">Type of the type.</typeparam>
152+
/// <param name="this">The @this to act on.</param>
153+
/// <param name="tryAction">The try action.</param>
154+
/// <param name="catchAction">The catch action.</param>
155+
/// <returns>true if it succeeds, false if it fails.</returns>
156+
public static bool Try<TType>(this TType @this, Action<TType> tryAction, Action<TType> catchAction)
157+
{
158+
try
159+
{
160+
tryAction(@this);
161+
return true;
162+
}
163+
catch
164+
{
165+
catchAction(@this);
166+
return false;
167+
}
168+
}
169+
}

src/Z.Core/Z.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@
471471
<Compile Include="System.Object\Utility\Object.CoalesceOrDefault.cs" />
472472
<Compile Include="System.Object\Utility\Object.GetValueOrDefault.cs" />
473473
<Compile Include="System.Object\Utility\Object.IfNotNull.cs" />
474+
<Compile Include="System.Object\Utility\Object.Try.cs" />
474475
<Compile Include="System.Object\Utility\Object.NullIf.cs" />
475476
<Compile Include="System.Object\Utility\Object.NullIfEquals.cs" />
476477
<Compile Include="System.Object\Utility\Object.NullIfEqualsAny.cs" />
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2015 ZZZ Projects. All rights reserved
2+
// Licensed under MIT License (MIT) (https://github.com/zzzprojects/Z.ExtensionMethods)
3+
// Website: http://www.zzzprojects.com/
4+
// Feedback / Feature Requests / Issues : http://zzzprojects.uservoice.com/forums/283927
5+
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
6+
7+
using System;
8+
using System.IO;
9+
10+
public static partial class Extensions
11+
{
12+
/// <summary>A DirectoryInfo extension method that copies to.</summary>
13+
/// <param name="obj">The obj to act on.</param>
14+
/// <param name="destDirName">Pathname of the destination directory.</param>
15+
public static void CopyTo(this DirectoryInfo obj, string destDirName)
16+
{
17+
obj.CopyTo(destDirName, "*.*", SearchOption.TopDirectoryOnly);
18+
}
19+
20+
/// <summary>A DirectoryInfo extension method that copies to.</summary>
21+
/// <param name="obj">The obj to act on.</param>
22+
/// <param name="destDirName">Pathname of the destination directory.</param>
23+
/// <param name="searchPattern">A pattern specifying the search.</param>
24+
public static void CopyTo(this DirectoryInfo obj, string destDirName, string searchPattern)
25+
{
26+
obj.CopyTo(destDirName, searchPattern, SearchOption.TopDirectoryOnly);
27+
}
28+
29+
/// <summary>A DirectoryInfo extension method that copies to.</summary>
30+
/// <param name="obj">The obj to act on.</param>
31+
/// <param name="destDirName">Pathname of the destination directory.</param>
32+
/// <param name="searchOption">The search option.</param>
33+
public static void CopyTo(this DirectoryInfo obj, string destDirName, SearchOption searchOption)
34+
{
35+
obj.CopyTo(destDirName, "*.*", searchOption);
36+
}
37+
38+
/// <summary>A DirectoryInfo extension method that copies to.</summary>
39+
/// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
40+
/// <param name="obj">The obj to act on.</param>
41+
/// <param name="destDirName">Pathname of the destination directory.</param>
42+
/// <param name="searchPattern">A pattern specifying the search.</param>
43+
/// <param name="searchOption">The search option.</param>
44+
public static void CopyTo(this DirectoryInfo obj, string destDirName, string searchPattern, SearchOption searchOption)
45+
{
46+
var files = obj.GetFiles(searchPattern, searchOption);
47+
foreach (var file in files)
48+
{
49+
var outputFile = destDirName + file.FullName.Substring(obj.FullName.Length);
50+
var directory = new FileInfo(outputFile).Directory;
51+
52+
if (directory == null)
53+
{
54+
throw new Exception("The directory cannot be null.");
55+
}
56+
57+
if (!directory.Exists)
58+
{
59+
directory.Create();
60+
}
61+
62+
file.CopyTo(outputFile);
63+
}
64+
65+
// Ensure empty dir are also copied
66+
var directories = obj.GetDirectories(searchPattern, searchOption);
67+
foreach (var directory in directories)
68+
{
69+
var outputDirectory = destDirName + directory.FullName.Substring(obj.FullName.Length);
70+
var directoryInfo = new DirectoryInfo(outputDirectory);
71+
if (!directoryInfo.Exists)
72+
{
73+
directoryInfo.Create();
74+
}
75+
}
76+
}
77+
}

src/Z.IO/Z.IO.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<Compile Include="System.Collections.Generic.IEnumerable[string]\IEnumerable[string].PathCombine.cs" />
6767
<Compile Include="System.Collections.Generic.IEnumerable[DirectoryInfo]\IEnumerable[DirectoryInfo].Delete.cs" />
6868
<Compile Include="System.Collections.Generic.IEnumerable[DirectoryInfo]\IEnumerable[DirectoryInfo].ForEach.cs" />
69+
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.CopyTo.cs" />
6970
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.Clear.cs" />
7071
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.CreateAllDirectories.cs" />
7172
<Compile Include="System.IO.DirectoryInfo\DirectoryInfo.DeleteDirectoriesWhere.cs" />

src/Z.Reflection/GetDeclaration/FieldInfo.GetDeclaration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static partial class Extensions
1818
public static string GetDeclaraction(this FieldInfo @this)
1919
{
2020
// Example: [Visibility] [Modifier] [Type] [Name] [PostModifier];
21-
2221
var sb = new StringBuilder();
2322

2423
// Variable

src/Z.Reflection/GetDeclaration/TypeInfo.GetDeclaration.cs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// All ZZZ Projects products: Entity Framework Extensions / Bulk Operations / Extension Methods /Icon Library
66

77
using System;
8+
using System.Collections.Generic;
89
using System.Linq;
910
using System.Reflection;
1011
using System.Text;
@@ -37,46 +38,80 @@ public static string GetDeclaraction(this Type @this)
3738
// Name
3839
sb.Append(@this.IsGenericType ? @this.Name.Substring(0, @this.Name.IndexOf('`')) : @this.Name);
3940

41+
List<string> constraintType = new List<string>();
42+
4043
// GenericArguments
4144
if (@this.IsGenericType)
4245
{
4346
Type[] arguments = @this.GetGenericArguments();
4447
sb.Append("<");
4548
sb.Append(string.Join(", ", arguments.Select(x =>
4649
{
47-
Type[] constraints = x.GetGenericParameterConstraints();
50+
GenericParameterAttributes sConstraints = x.GenericParameterAttributes;
4851

49-
foreach (Type constraint in constraints)
52+
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.Contravariant))
53+
{
54+
sb.Append("in ");
55+
}
56+
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.Covariant))
5057
{
51-
GenericParameterAttributes gpa = constraint.GenericParameterAttributes;
52-
GenericParameterAttributes variance = gpa & GenericParameterAttributes.VarianceMask;
58+
sb.Append("out ");
59+
}
5360

54-
if (variance != GenericParameterAttributes.None)
55-
{
56-
sb.Append((variance & GenericParameterAttributes.Covariant) != 0 ? "in " : "out ");
57-
}
61+
List<string> parameterConstraint = new List<string>();
62+
63+
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.ReferenceTypeConstraint))
64+
{
65+
parameterConstraint.Add("class");
66+
}
67+
68+
69+
if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.DefaultConstructorConstraint))
70+
{
71+
parameterConstraint.Add("new()");
72+
}
73+
74+
75+
if (parameterConstraint.Count > 0)
76+
{
77+
constraintType.Add(x.Name + " : " + string.Join(", " , parameterConstraint));
5878
}
5979

6080
return x.GetShortDeclaraction();
6181
})));
6282
sb.Append(">");
83+
84+
foreach (var argument in arguments)
85+
{
86+
GenericParameterAttributes sConstraints = argument.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
87+
}
6388
}
6489

90+
List<string> constaints = new List<string>();
91+
6592
// Inherited Class
6693
if (@this.BaseType != null && @this.BaseType != typeof (object))
6794
{
68-
hasInheritedClass = true;
69-
70-
sb.Append(" : ");
71-
sb.Append(@this.BaseType.GetShortDeclaraction());
95+
constaints.Add(@this.BaseType.GetShortDeclaraction());
7296
}
73-
97+
7498
// Inherited Interface
7599
Type[] interfaces = @this.GetInterfaces();
76100
if (interfaces.Length > 0)
77101
{
78-
sb.Append(hasInheritedClass ? ", " : " : ");
79-
sb.Append(string.Join(", ", interfaces.Select(x => x.Name)));
102+
constaints.AddRange(interfaces.Select(x => x.Name));
103+
}
104+
105+
if (constaints.Count > 0)
106+
{
107+
sb.Append(" : ");
108+
sb.Append(string.Join(", ", constaints));
109+
}
110+
111+
if (constraintType.Count > 0)
112+
{
113+
sb.Append(" where ");
114+
sb.Append(string.Join(", ", constraintType));
80115
}
81116

82117
return sb.ToString();

0 commit comments

Comments
 (0)