public static string EscapeString(string source, string[] replaceString, Func <char, int> escapeFunc) { if (string.IsNullOrEmpty(source)) { return(string.Empty); } AtawDebug.AssertArgumentNull(escapeFunc, "escapeFunc", null); AtawDebug.AssertEnumerableArgumentNull <string>(replaceString, "replaceString", null); StringBuilder cachedStringBuilder = null; int start = 0; int pos = 0; int count = source.Length; for (int i = 0; i < count; ++i) { pos = escapeFunc(source[i]); if (pos < 0) { continue; } if (cachedStringBuilder == null) { cachedStringBuilder = new StringBuilder(); } cachedStringBuilder.Append(source.Substring(start, i - start)); AtawDebug.Assert(pos < replaceString.Length, string.Format(ObjectUtil.SysCulture, "参数escapeFunc返回的值有误,replaceString的长度为{0},但是返回值却是{1},越界了", replaceString.Length, pos), null); cachedStringBuilder.Append(replaceString[pos]); start = i + 1; } if (start == 0) { return(source); } else if (start < count) { cachedStringBuilder.Append(source.Substring(start, count - start)); } string s = cachedStringBuilder.ToString(); return(s); }
public static object CreateObject(Type type, params object[] args) { AtawDebug.AssertArgumentNull(type, "type", null); AtawDebug.AssertEnumerableArgumentNull(args, "args", null); try { return(Activator.CreateInstance(type, args)); } catch (MissingMethodException ex) { AtawDebug.ThrowAtawException(string.Format(ObjectUtil.SysCulture, "没有找到类型{0}参数为{1}的构造函数,请确认", type, GetMethodSign(args)), ex, null); } catch (TargetInvocationException ex) { AtawDebug.ThrowAtawException(string.Format(ObjectUtil.SysCulture, "调用类型{0}参数为{1}的构造函数时,发生例外,请调试你的代码", type, GetMethodSign(args)), ex, null); } catch (MethodAccessException ex) { AtawDebug.ThrowAtawException(string.Format(ObjectUtil.SysCulture, "类型{0}参数为{1}的构造函数访问权限不够", type, GetMethodSign(args)), ex, null); } catch (MemberAccessException ex) { AtawDebug.ThrowAtawException(string.Format(ObjectUtil.SysCulture, "类型{0}可能是一个抽象类型,无法创建", type), ex, null); } catch (Exception ex) { AtawDebug.ThrowAtawException(string.Format(ObjectUtil.SysCulture, "调用类型{0}参数为{1}的构造函数是发生例外", type, GetMethodSign(args)), ex, null); } return(null); }