Reputation: 2659
I have a Serializar helper class that will deserialize some xml for me. I also have an Interface called IStorageService that has two implementation.
here is my IStorageService interface :
public interface IStorageService
{
string GetFullImageUri(string fileName);
}
Here is the two implementation :
1-
public class AzureBlobStorageService : IStorageService
{
private readonly string _rootPath;
public string GetFullImageUri(string fileName)
{
return _rootPath + fileName;
}
}
2-
public class FileSystemStorageService : IStorageService
{
public string GetFullImageUri(string fileName)
{
return _applicationPath + "/Data/Images/"+ fileName;
}
}
Here is my Serializar class
public class Serializar
{
private readonly IStorageService _storageService;
public Serializar(IStorageService storageService)
{
_storageService = storageService;
}
public static List<ProductType> DeserializeXmlAsProductTypes(object xml)
{
// do stuff here.
// this method require using _storageService.GetFullImageUri("");
}
}
I am getting this compiling error :
Error 32 An object reference is required for the non-static field, method, or property 'Serializar._storageService
How to resolve this in the IocConfig.cs using Autofac ?
Upvotes: 1
Views: 267
Reputation: 172606
You can't solve this with Autofac. The problem is in your code and the C# compiler tells you what's wrong.
The problem is that your DeserializeXmlAsProductTypes
method is static, but you try to access instance field. This is not possible in .NET and the C# compiler therefore presents you with an error.
The solution is to make the DeserializeXmlAsProductTypes
an instance method, simply by removing the static
keyword from the method definition.
This might however cause other code in your application to fail, because there might be some code that depends on this static method. If this is the case, the solution here is to inject the Serializar
into the constructor of such class, so that the failing code can make use of the Serializar
instance and call the new DeserializeXmlAsProductTypes
instance method.
Upvotes: 2