I have built a custom regular expression class. I also have a database value that is a list of characters that I don't want to be allowed anywhere in my web application. My custom regular expression class will take all other regular expressions and ensure that my list of unwanted characters are not allowed. My custom regular expression class is register in my Global.asax.cs and since it is a value in the database it can be changed when deemed necessary. Now what I need to do is find a way to take the regex error message and add on to it with something like: "This field cannot contain the following: " + mybadcharacterlist;
Already tried this:
public const string AlphaErrMsg = "This field can only contain letters. This field cannot include the following characters: " + RestrictedCharacterList.GetList();
Which didn't work because the error parameter for RegularExpressionAttribute requires a const and calling my GetList method isn't a constant.
protected void Application_Start()
{
DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof (RestrictCharRegExpressAttribute), typeof(RegulatExpressionAttributeAdapter);
}
public class RestrictCharRegExpressAttribute : RegularExpressionAttribute
{
public RestrictCharRegExpressAttribute(string propRegex) : base(GetRegex(propRegex)) {}
private static string GetRegex(string propRegex)
{
in indexPropRegex = propRegex.IndexOf('^');
string restrictedCharsAction = "(?!.*[" + RestrictedCharacterList.GetList() + "])";
propRegex = indexPropRegex == -1 ? propRegex.Insert(0, restrictedCharsAction) : propRegex.Insert(indexPropRegex + 1, restrictedCharsAction);
return propRegex;
}
}
public static class RestrictedCharacterList
{
public static string GetList()
{
string restrictedChars;
if (HttpContext.Current?.Session == null)
{
restrictedChars = EnvironmentSettingsDA.GetSetting(AppConfiguration.Settings.ConnectionString, "CAMPS", "RESTRICTED_CHARACTERS");
}
else
{
restrictedChars = HttpContext.Current.Session.GetDataFromSession<string>("RESTRICTED_CHARACTERS");
if (restrictedChars == null)
{
restrictedChars = EnvironmentSettingsDA.GetSetting(AppConfiguration.Settings.ConnectionString, "CAMPS", "RESTRICTED_CHARACTERS");
HttpContext.Current.Session.SetDataToSession<string>("Restricted_Characters", restrictedChars);
}
}
return restrictedChars;
}
}
public class User
{
public const string IsAlphaRegex = "^[a-zA-Z]*$'
public const string AlphaErrMsg = "This field can only contain letters.";
[RestrictCharRegExpress(IsAlphaRegex, ErrorMessage = AlphaErrMsg)]
public string FirstName { get; set; }
}
The expected results would be to add a message to all my regex error messages describing what characters are not allowed while ensuring that list of characters can be changed in the database.
The way you do this is to override FormatErrorMessage in RestrictCharRegExprAttribute:
public class RestrictCharRegExprAttribute: RegularExpressionAttribute
{
public RestrictCharRegExpressAttribute(string propRegex): base( GetRegex(propRegex)
{
this.Message = ...; // localized message
}
private string Message { get; }
public override string FormatErrorMessage(string propertyName)
{
return this.Message;
}
}
Special thanks to Reviews Alot for leading me down the right path but this is what I did to accomplish what I was trying to do.
Added the following to RestrictedCharacterList class:
public static class RestrictedCharacterList
{
public static string GetErrorMessage()
{
return " Valid values cannot include the following restricted characters: " + GetList();
}
}
Added the following to RestrictCharRegExpressAttribute class:
public class RestrictCharRegExpressAttribute : RegularExpressionAttribute
{
public string AddToRestrictCharErrorMessage { get; set; }
public override string FormatErrorMessage(string name)
{
if (string.isNullOrWhiteSpace(AddToRestrictedCharErrorMessage))
{
return base.FormatErrorMessage(name);
}
else
{
return AddToRestrctCharErrorMessage + " " + RestrictedCharacterList.GetErrorMessage():
}
}
}
And then when you want the special character message appended to an existing error message you assign the data attribute like so:
public class User
{
public const string IsAlphaRegex = "^[a-zA-Z]*$'
public const string AlphaErrMsg = "This field can only contain letters.";
[RestrictCharRegExpress(IsAlphaRegex, AddToRestrictCharErrorMessage = AlphaErrMsg)]
public string FirstName { get; set; }
}
I receive this kind of error:
int does not contain a definition for 'childConvert' and no accessible extension method 'childconver' accepting a first argument of type'int' could be found (are you missing assembly reference)
In Main Method:
int n = 10;
string Name = n.ChildConver();
In Child Method:
public static string ChildConver(this int Name)
{
string Namecovert = Convert.ToString(Name) + "Convertion";
return Namecovert;
}
Try to put it in a static class.
public static class Common
{
public static string ChildConver(this int name)
{
return name + "Convertion";
}
}
Extension methods must be defined in a non-generic static class.
Ref: MSDN
Define extension method in separate class.
public static class IntHelper
{
public static string ChildConver(this int Name)
{
string Namecovert = Convert.ToString(Name) + "Convertion";
return Namecovert;
}
}
public static string ChildConver(this int Name)
{
retunt Name + "Convertion";
}
Try ToString()
public class Program
{
public static void Main()
{
int n = 10;
string Name = n.ChildConver();
System.Console.WriteLine(Name);
}
}
public static class Ext
{
public static string ChildConver(this int Name)
{
string Namecovert = Name.ToString() + " Convertion";
return Namecovert;
}
}
Hello I have a lot of variables as I mentioned below, but when I change the value of a variable that I refer to, this change is not adapted by the variables that use this variable.
class PublicVariable
{
public static string ActiveUser = UserInfo.Username;
public static string ActiveUserPath = $#"{Application.StartupPath}\{ActiveUser}";
public static string ActiveUserImg = $#"{ActiveUserPath}\User.png";
}
class UserInfo
{
public static string Username = "-1064548"; //Working
}
class Starting
{
public void Login(string Username, string Pwd)
{
//After the user logged in.
UserInfo.Username = "BruceWayne"; //Working
/* Showing -1064548 = */ MessageBox.Show(PublicVariable.ActiveUser.ToString()); //Not Working.
}
}
For simplicity of the code, ActiveUser is an example.
This code sequence is an example. The goal is to take the data from the database once.
To solve your problem I'd suggest to use properties here. This would look like this then:
class PublicVariable
{
public static string ActiveUser => UserInfo.Username;
public static string ActiveUserPath => $#"{Application.StartupPath}\{ActiveUser}";
public static string ActiveUserImg => $#"{ActiveUserPath}\User.png";
}
class UserInfo
{
public static string Username = "-1064548"; //Working
}
class Starting
{
public void Login (string Username, string Pwd)
{
UserInfo.Username = "BruceWayne";
MessageBox.Show (PublicVariable.ActiveUser.ToString ());
}
}
Alternatively you could use methods as well:
class PublicVariable
{
public static string ActiveUser() => UserInfo.Username;
public static string ActiveUserPath() => $#"{Application.StartupPath}\{ActiveUser()}";
public static string ActiveUserImg() => $#"{ActiveUserPath()}\User.png";
}
class UserInfo
{
public static string Username = "-1064548"; //Working
}
class Starting
{
public void Login (string Username, string Pwd)
{
UserInfo.Username = "BruceWayne";
MessageBox.Show (PublicVariable.ActiveUser().ToString ());
}
}
There actually isn't any major difference between properties and methods. However, fields (you used them) are different, as they don't update their value when the depending value changes. That means, as long as you don't have a reference to the other value (which would be the case for objects, like an object of your class Starting), the field value doesn't update.
private static void Main(string[] args)
{
string str = null;
Logger.InitUserLogWithRotation(); // <--- error occur
...
}
When I build project, it has no error. But When I execute it, it always aborted.
I tried to debug project , but System.TypeInitializationException error occurred at first line.
I've already tried to googling , yet found no solution.
It seems like any variable initialize code is wrong , but can't find it.
Please help me. I'm new to C#.
Thanks.
※ Here is Logger Class Code
public class Logger
{
private static int HDLOG_PRIORITY_DEBUG = 4;
private static int HDLOG_PRIORITY_ERROR = 1;
private static int HDLOG_PRIORITY_FATAL = 0;
private static int HDLOG_PRIORITY_INFO = 3;
private static int HDLOG_PRIORITY_WARNING = 2;
public static int LOG_LEVEL_DEBUG = 4;
public static int LOG_LEVEL_ERROR = 2;
public static int LOG_LEVEL_FATAL = 1;
public static int LOG_LEVEL_INFO = 5;
public static int LOG_LEVEL_WARNING = 3;
private static string s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
private static string s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
private static string s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
private static bool s_consoleLogging = false;
private static FileStream s_fileStream;
public static HdLoggerCallback s_HdLoggerCallback;
private static string s_logDir = null;
private static string s_logFileName = "XXXX";
private static string s_logFilePath = null;
public static int s_logFileSize = 0xa00000;
private static bool s_loggerInited = false;
private static string s_logLevels = null;
private static int s_logRotationTime = 0x7530;
private static string s_logStringDebug = "DEBUG";
private static string s_logStringError = "ERROR";
private static string s_logStringFatal = "FATAL";
private static string s_logStringInfo = "INFO";
private static string s_logStringWarning = "WARNING";
private static int s_processId = -1;
private static string s_processName = "Unknown";
private static object s_sync = new object();
public static int s_totalLogFileNum = 5;
private static TextWriter writer = Console.Error;
private static void Close()
{
if (!s_consoleLogging)
{
writer.Close();
s_fileStream.Dispose();
writer.Dispose();
}
}
public static void Debug(string msg)
{
Debug("{0}", new object[] { msg });
}
public static void Debug(string fmt, params object[] args)
{
Print(LOG_LEVEL_DEBUG, s_processName, fmt, args);
}
private static void DoLogRotation()
{
Label_0000:
Thread.Sleep(s_logRotationTime);
try
{
lock (s_sync)
{
FileInfo info = new FileInfo(s_logFilePath);
if (info.Length >= s_logFileSize)
{
string destFileName = s_logFilePath + ".1";
string path = s_logFilePath + "." + s_totalLogFileNum;
if (File.Exists(path))
{
File.Delete(path);
}
for (int i = s_totalLogFileNum - 1; i >= 1; i--)
{
string str3 = s_logFilePath + "." + i;
string str4 = s_logFilePath + "." + (i + 1);
if (File.Exists(str3))
{
File.Move(str3, str4);
}
}
File.Move(s_logFilePath, destFileName);
}
}
goto Label_0000;
}
catch (Exception)
{
goto Label_0000;
}
}
public static void Error(string msg)
{
Error("{0}", new object[] { msg });
}
public static void Error(string fmt, params object[] args)
{
Print(LOG_LEVEL_ERROR, s_processName, fmt, args);
}
public static void Fatal(string msg)
{
Fatal("{0}", new object[] { msg });
}
public static void Fatal(string fmt, params object[] args)
{
Print(LOG_LEVEL_FATAL, s_processName, fmt, args);
}
private static string GetLogDir(bool userSpecificLog)
{
string str;
if (s_logDir != null)
{
return s_logDir;
}
try
{
if (userSpecificLog)
{
str = Path.Combine(s_bstUserDataDir, "Logs");
}
else
{
str = (string) Registry.LocalMachine.OpenSubKey(#"Software\XXXX").GetValue("LogDir");
}
}
catch (Exception)
{
str = Path.Combine(s_bstUserDataDir, "Logs");
}
s_logDir = str;
return str;
}
private static string GetPrefix(string tag, string logLevel)
{
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
DateTime now = DateTime.Now;
return string.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}.{6:D3} {7}:{8:X8} ({9}). {10}: ", new object[] { now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, now.Millisecond, s_processId, managedThreadId, tag, logLevel });
}
public static TextWriter GetWriter()
{
return new Writer(delegate (string msg) {
Print(msg);
});
}
private static void HdLogger(int prio, uint tid, string tag, string msg)
{
int level = 0;
if (prio == HDLOG_PRIORITY_FATAL)
{
level = LOG_LEVEL_FATAL;
}
else if (prio == HDLOG_PRIORITY_ERROR)
{
level = LOG_LEVEL_ERROR;
}
else if (prio == HDLOG_PRIORITY_WARNING)
{
level = LOG_LEVEL_WARNING;
}
else if (prio == HDLOG_PRIORITY_INFO)
{
level = LOG_LEVEL_INFO;
}
else if (prio == HDLOG_PRIORITY_DEBUG)
{
level = LOG_LEVEL_DEBUG;
}
Print(level, tag, "{0:X8}: {1}", new object[] { tid, msg });
}
public static void Info(string msg)
{
Info("{0}", new object[] { msg });
}
public static void Info(string fmt, params object[] args)
{
Print(LOG_LEVEL_INFO, s_processName, fmt, args);
}
public static void InitConsoleLog()
{
InitLog("-", true, false);
}
public static void InitLog(string logFileName, bool userSpecificLog, bool doLogRotation)
{
s_loggerInited = true;
s_HdLoggerCallback = new HdLoggerCallback(Logger.HdLogger);
s_processId = Process.GetCurrentProcess().Id;
s_processName = Process.GetCurrentProcess().ProcessName;
if (logFileName == "-")
{
writer = Console.Error;
s_consoleLogging = true;
}
else
{
if (logFileName == null)
{
logFileName = s_logFileName;
}
if (userSpecificLog)
{
logFileName = logFileName + "Users";
}
string logDir = GetLogDir(userSpecificLog);
string str2 = string.Format(#"{0}\{1}.log", logDir, logFileName);
if (!Directory.Exists(logDir))
{
Directory.CreateDirectory(logDir);
}
s_logFilePath = str2;
LogLevelsInit();
lock (s_sync)
{
Open();
}
if (doLogRotation)
{
new Thread(() => DoLogRotation()) { IsBackground = true }.Start();
}
}
}
public static void InitSystemLog()
{
InitLog(null, false, false);
}
public static void InitSystemLogWithRotation()
{
InitLog(null, false, true);
}
public static void InitUserLog()
{
InitLog(null, true, false);
}
public static void InitUserLogWithRotation()
{
InitLog(null, true, true);
}
private static bool IsLogLevelEnabled(string tag, string level)
{
if (s_logLevels == null)
{
return false;
}
return (s_logLevels.StartsWith("ALL") || s_logLevels.Contains((tag + ":" + level).ToUpper()));
}
private static void LogLevelsInit()
{
string name = #"Software\XXXX\Config";
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(name))
{
s_logLevels = (string) key.GetValue("DebugLogs");
}
}
catch (Exception)
{
return;
}
if (s_logLevels != null)
{
s_logLevels = s_logLevels.ToUpper();
}
}
private static void Open()
{
if (!s_consoleLogging)
{
if (!s_loggerInited)
{
InitLog("-", false, false);
s_loggerInited = true;
}
else
{
s_fileStream = new FileStream(s_logFilePath, FileMode.Append, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite);
writer = new StreamWriter(s_fileStream, Encoding.UTF8);
}
}
}
public static void Print(string msg)
{
Print("{0}", new object[] { msg });
}
public static void Print(string fmt, params object[] args)
{
Print(LOG_LEVEL_INFO, s_processName, fmt, args);
}
public static void Print(int level, string tag, string fmt, params object[] args)
{
string str = "UNKNOWN";
if (level == LOG_LEVEL_FATAL)
{
str = s_logStringFatal;
}
else if (level == LOG_LEVEL_ERROR)
{
str = s_logStringError;
}
else if (level == LOG_LEVEL_WARNING)
{
str = s_logStringWarning;
}
else if (level == LOG_LEVEL_INFO)
{
str = s_logStringInfo;
}
else if (level == LOG_LEVEL_DEBUG)
{
str = s_logStringDebug;
}
if ((level != LOG_LEVEL_DEBUG) || IsLogLevelEnabled(tag, str))
{
lock (s_sync)
{
Open();
writer.WriteLine(GetPrefix(tag, str) + fmt, args);
writer.Flush();
Close();
}
}
}
public static void SetLogDir(string logDir)
{
s_logDir = logDir;
}
public static void Warning(string msg)
{
Warning("{0}", new object[] { msg });
}
public static void Warning(string fmt, params object[] args)
{
Print(LOG_LEVEL_WARNING, s_processName, fmt, args);
}
public delegate void HdLoggerCallback(int prio, uint tid, string tag, string msg);
public class Writer : TextWriter
{
private WriteFunc writeFunc;
public Writer(WriteFunc writeFunc)
{
this.writeFunc = writeFunc;
}
public override void WriteLine(string msg)
{
this.writeFunc(msg);
}
public override void WriteLine(string fmt, object obj)
{
this.writeFunc(string.Format(fmt, obj));
}
public override void WriteLine(string fmt, object[] objs)
{
this.writeFunc(string.Format(fmt, objs));
}
public override System.Text.Encoding Encoding
{
get
{
return System.Text.Encoding.UTF8;
}
}
public delegate void WriteFunc(string msg);
}
}
Whenever a TypeInitializationException is thrown, check all initialization logic of the type you are referring to for the first time in the statement where the exception is thrown - in your case: Logger.
Initialization logic includes: the type's static constructor (which - if I didn't miss it - you do not have for Logger) and field initialization.
Field initialization is pretty much "uncritical" in Logger except for the following lines:
private static string s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
private static string s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
private static string s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
s_commonAppData is null at the point where Path.Combine(s_commonAppData, "XXXX"); is called. As far as I'm concerned, these initializations happen in the exact order you wrote them - so put s_commonAppData up by at least two lines ;)
System.TypeInitializationException happens when the code that gets executed during the process of loading the type throws an exception.
When .NET loads the type, it must prepare all its static fields before the first time that you use the type. Sometimes, initialization requires running code. It is when that code fails that you get a System.TypeInitializationException.
In your specific case, the following three static fields run some code:
private static string s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
private static string s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
private static string s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
Note that s_bstCommonAppData depends on s_commonAppData, but it is declared ahead of its dependency. Therefore, the value of s_commonAppData is null at the time that the Path.Combine is called, resulting in ArgumentNullException. Same goes for the s_bstUserDataDir and s_bstCommonAppData: they are declared in reverse order to the desired order of initialization.
Re-order the lines to fix this problem:
private static string s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
private static string s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
private static string s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
These lines are your problem (or at least one of your problems, if there are more):
private static string s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
private static string s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
private static string s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
You reference some static members in the initializers for other static members. This is a bad idea, as the compiler doesn't know in which order to initialize them. The result is that during the initialization of s_bstCommonAppData, the dependent field s_commonAppData has not yet been initialized, so you are calling Path.Combine(null, "XXXX") and this method does not accept null arguments.
You can fix this by making sure that fields used in the initialization of other fields are declared first:
private static string s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
private static string s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
private static string s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
Or use a static constructor to explicitly order the assignments:
private static string s_bstCommonAppData;
private static string s_bstUserDataDir;
private static string s_commonAppData;
static Logger()
{
s_commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
s_bstCommonAppData = Path.Combine(s_commonAppData, "XXXX");
s_bstUserDataDir = Path.Combine(s_bstCommonAppData, "UserData");
}
i. Please check the InnerException property of the TypeInitializationException
ii. Also, this may occur due to mismatch between the runtime versions of the assemblies. Please verify the runtime versions of the main assembly (calling application) and the referred assembly
I know that this is a bit of an old question, but I had this error recently so I thought I would pass my solution along.
My errors seem to stem from a old App.Config file and the "in place" upgrade from .Net 4.0 to .Net 4.5.1.
When I started the older project up after upgrading to Framework 4.5.1 I got the TypeInitializationException... right off the bat... not even able to step through one line of code.
After creating a brand new wpf project to test, I found that the newer App.Config file wants the following.
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="YourAppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
Once I dropped that in, I was in business.
Note that your need might be slightly different. I would create a dummy project, check out the generated App.Config file and see if you have anything else missing.
Hope this helps someone. Happy Coding!
I experienced the System.TypeInitializationException due to a different error in my .NET framework 4 project's app.config. Thank you to pStan for getting me to look at the app.config. My configSections were properly defined. However, an undefined element within one of the sections caused the exception to be thrown.
Bottom line is that problems in the app.config can generated this very misleading TypeInitializationException.
A more meaningful ConfigurationErrorsException can be generated by the same error in the app.config by waiting to access the config values until you are within a method rather than at the class level of the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
namespace ConfigTest
{
class Program
{
public static string machinename;
public static string hostname;
public static NameValueCollection _AppSettings;
static void Main(string[] args)
{
machinename = System.Net.Dns.GetHostName().ToLower();
hostname = "abc.com";// System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).HostName.ToLower().Replace(machinename + ".", "");
_AppSettings = ConfigurationManager.GetSection("domain/" + hostname) as System.Collections.Specialized.NameValueCollection;
}
}
}
If you have custom attributes in appsetting. move your
<configSections>
</configSections>
tag to the first child in the <confuguration>.
The TypeInitializationException that is thrown as a wrapper around the exception thrown by the class initializer. This class cannot be inherited.
TypeInitializationException is also called static constructors.
I have the error of system.typeintialzationException, which is due to when I tried to move the file like:
File.Move(DestinationFilePath, SourceFilePath)
That error was due to I had swapped the path actually, correct one is:
File.Move(SourceFilePath, DestinationFilePath)
I had this problem. As stated it is probably a static declaration issue. In my case it was because I had a static within a DEBUG clause. That is (in c#)
#if DEBUG
public static bool DOTHISISINDEBUGONLY = false;
#endif
Everything worked fine until I complied a Release version of the code and after that I got this error - even on old release versions of the code. Once I took the variable out of the DEBUG clause everything returned to normal.
Had a simular issue getting the same exception. Took some time locating. In my case I had a static utility class with a constructor that threw the exception (wrapping it). So my issue was in the static constructor.
For me it was simple function like Dal.isPhoneNumberInUse("some string number") that threw this exception. what was weired to me because the Dal was in use several times a few lines before.
Finally I relized that the problem was the parameter that was sent into this function, it was dynamic and I was suppose to parse it to string.
:
Dal.isPhoneNumberInUse(data.phoneNumber) //not good
Dal.isPhoneNumberInUse((string)data.phoneNumber);//good
of course you need to verify that data.phoneNumber is type of string/object and not a struct