C# Compiler Error
CS0572 – ‘type’ : cannot reference a type through an expression; try ‘path_to_type’ instead
Reason for the Error
You will get this error in your C# code when you are trying to access the member of a class through an identifier that is not permitted.
For example, let’s compile the below C# program
using System; namespace DeveloperPublishConsoleCore { class Parent { public class InnerParent { public static int ID = 100; } } class Child : Parent { public void WriteData() { Parent obj = new Parent(); obj.InnerParent.ID = 100; } } internal class Program { static void Main(string[] args) { Console.WriteLine("DeveloperPublish Hello World!"); } } }
You will receive the error code CS0572 because you are trying to access the member (ID) of the InnerParent class through the identifier.
Error CS0572 ‘InnerParent’: cannot reference a type through an expression; try ‘Parent.InnerParent’ instead DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 17 Active
Solution
You can fix this error by ensuring that you are trying to access the member in the right way. For example, the error in the above code can be fixed by calling Parent.InnerParent.ID = 10.