Muthukumar Palaniappan Muthukumar Palaniappan - 2 months ago 23
C# Question

C# assigning Func to an event

I am trying to create a filehelper utility and I am getting issue in assigning Func to an event. It says non assignable.

The event and delegate signature is as below

public event AfterReadHandler<T> AfterReadRecord
public delegate void AfterReadHandler<T>(EngineBase engine, fterReadEventArgs<T> e);


The code is as below

public class FileHelperUtility<T> where T : class
{

public List<T> ParseFile<T>(string fileName, ImportFileError fileError,Func<EngineBase, AfterReadEventArgs<T> > afterReadFunc ) where T : class
{
List<T> records = new List<T>();
var fileEngine = InitializeFileEngine<T>(afterReadFunc);
records = fileEngine.ReadFile(fileName).ToList();
if (ValidateHeader(fileEngine.HeaderText))
{
fileError.ErrorType = ImportFileFaultType.InvalidHeaderRecordType;
fileError.ErrorMessage = "No header record in the file.";
}
else
{
PopulateErrors(fileError, fileEngine.ErrorManager);
}
return records;
}

private FileHelperEngine<T> InitializeFileEngine<T>(Func<EngineBase, AfterReadEventArgs<T>> afterReadFunc) where T : class
{
var fileEngine = new FileHelperEngine<T>(Encoding.Default);
fileEngine.ErrorMode = ErrorMode.SaveAndContinue;
if (afterReadFunc != null)
{
fileEngine.AfterReadRecord += afterReadFunc;
}
return fileEngine;
}


private void PopulateErrors(ImportFileError fileError, ErrorManager errorManager)
{
if (errorManager.Errors.Count() > 0)
{
fileError.ErrorType = ImportFileFaultType.InvalidDetailRecordType;
fileError.ErrorData = new List<string>();
}
foreach (var error in errorManager.Errors)
{
string errorString = string.Format("Line:{0} Field:{1} - ErrorInfo:{2}",
error.LineNumber,
((ConvertException)error.ExceptionInfo).FieldName,
error.ExceptionInfo.InnerException);
fileError.ErrorData.Add(errorString);
}

}
private bool ValidateHeader(string headerRecord)
{
bool isheaderValid;
if (!string.IsNullOrEmpty(headerRecord) &&
!string.IsNullOrEmpty(headerRecord.Trim(new char[] { '\r', '\n' })))
{
isheaderValid = true;
}
else
{
isheaderValid = false;
}
return isheaderValid;
}



}


I am getting the non- assignable error at
fileEngine.AfterReadRecord += afterReadFunc;


Can Someone help.

Answer

I've cut your code down slightly, but the issue is that an AfterReadHandler<T> isn't an Func<EngineBase, AfterReadEventArgs<T>> (nor is it a Action<EngineBase, AfterReadEventArgs<T>>.

You have two choices to fix your code.

(1)

private FileHelperUtility<T> InitializeFileEngine<T>(AfterReadHandler<T> afterReadFunc) where T : class
{
    var fileEngine = new FileHelperUtility<T>();
    if (afterReadFunc != null)
    {
        fileEngine.AfterReadRecord += afterReadFunc;
    }
    return fileEngine;
}

(2)

private FileHelperUtility<T> InitializeFileEngine<T>(Action<EngineBase, AfterReadEventArgs<T>> afterReadFunc) where T : class
{
    var fileEngine = new FileHelperUtility<T>();
    if (afterReadFunc != null)
    {
        fileEngine.AfterReadRecord += (eb, area) => afterReadFunc(eb, area);
    }
    return fileEngine;
}

Let me know if this isn't clear enough.