I have 2 classes, one is a DLL and the other is a.cs File and i want to call a Method in the.cs File from the DLL.
DLL
namespace SimpleDebugFormatting
{
public static class DebugLog
{
private static Type debugFormat;
private static Type DebugFormat
{
get
{
if (debugFormat == null)
return debugFormat = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(_Type => _Type.Name.Contains("DebugFormat"));
return debugFormat;
}
}
public static List<string> GetMessages(IEnumerable _Parameters)
{
return (List<string>)DebugFormat.GetMethod("ConcatMessages", BindingFlags.Static | BindingFlags.NonPublic).Invoke(DebugFormat, new object[] { _Parameters });
}
}
}
.cs File
namespace SimpleDebugFormatting
{
internal static class DebugFormat
{
internal static List<string> ConcatMessages(object[] _Messages) { }
}
}
When both classes are.cs files it all works fine, but when the "DebugLog"-class is a DLL it can't find the "DebugFormat"-class.
When i try to print all types in the DLL's assembly with
" Assembly.GetExecutingAssembly().GetTypes() ",
it only shows these 2:
SimpleDebugFormatting.DebugLog
SimpleDebugFormatting.DebugLog+<>c
Why is that so and how can i get it to work?
Your DebugFormat
class is marked as internal
Internal types or members are accessible only within files in the same assembly, as in this example:
So, you specifically asked DebugFormat
not to be accessible by DebugLog
. Make it public if you want it to be accessible.
Fixed it with this:
private static Type DebugFormat
{
get
{
if (debugFormat == null)
return debugFormat = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(
_Assembly => _Assembly.FullName.Contains("SimpleDebugFormattingAssembly")).GetTypes().FirstOrDefault(
_Type => _Type.Name.Contains("DebugFormat"));
return debugFormat;
}
}
@JonasH, @Selvin, thx for the quick replies.
I went through a similar situation, and I found the class in this way. Write down the code to help not only the questioner but also others who see this article.
public static Type DebugFormat
{
get
{
if (debugFormat == null)
{
return debugFormat = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(
_Assembly => _Assembly.FullName.Contains("Assembly-CSharp")).GetTypes()
.FirstOrDefault(_Type => _Type.Name.Contains("namespace.classname"));
}
return debugFormat;
}
}
Sorry for not being good at English In addition, I also wrote a method to use regarding GetType()
Type assemblyType = Type.GetType("namespace.classname, Assembly-CSharp");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.