How to access internal class using Reflection
In general, you shouldn't do this - if a type has been marked internal, that means you're not meant to use it from outside the assembly. It could be removed, changed etc in a later version.
However, reflection does allow you to access types and members which aren't public - just look for overloads which take a BindingFlags
argument, and include BindingFlags.NonPublic
in the flags that you pass.
If you have the fully qualified name of the type (including the assembly information) then just calling Type.GetType(string)
should work. If you know the assembly in advance, and know of a public type within that assembly, then using typeof(TheOtherType).Assembly
to get the assembly reference is generally simpler, then you can call Assembly.GetType(string)
.
Is it possible to access internal class with reflection in Kotlin?
You can use Suppress annotation in your class with following names: "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE" like this:
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
You can see an example here: https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-debug/src/DebugProbes.kt#L5
In this class, an internal object DebugProbesImpl is accessed.
How can I use reflection to invoke a method on this internal class that exists in a library?
You can use the InternalsVisibleToAttribute to make the internal class visible to your unit test project.
https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx
How to use reflection to determine if a class is internal?
This is a classic issue. From MSDN:
The C# keywords
protected
andinternal
have no meaning in IL and are not used in the Reflection APIs. The corresponding terms in IL areFamily
andAssembly
. To identify aninternal
method using Reflection, use theIsAssembly
property. To identify aprotected internal
method, use theIsFamilyOrAssembly
.
Reflection does not expose a way on Type
check if it is internal
, protected
or protected internal
.
How can I access an internal class from an external assembly?
Without access to the type (and no "InternalsVisibleTo" etc) you would have to use reflection. But a better question would be: should you be accessing this data? It isn't part of the public type contract... it sounds to me like it is intended to be treated as an opaque object (for their purposes, not yours).
You've described it as a public instance field; to get this via reflection:
object obj = ...
string value = (string)obj.GetType().GetField("test").GetValue(obj);
If it is actually a property (not a field):
string value = (string)obj.GetType().GetProperty("test").GetValue(obj,null);
If it is non-public, you'll need to use the BindingFlags
overload of GetField
/GetProperty
.
Important aside: be careful with reflection like this; the implementation could change in the next version (breaking your code), or it could be obfuscated (breaking your code), or you might not have enough "trust" (breaking your code). Are you spotting the pattern?
Accessing internal members via System.Reflection?
It would be more appropriate to use the InternalsVisibleTo
attribute to grant access to the internal members of the assembly to your unit test assembly.
Here is a link with some helpful additional info and a walk through:
- The Wonders Of InternalsVisibleTo
To actually answer your question... Internal and protected are not recognized in the .NET Reflection API. Here is a quotation from MSDN:
The C# keywords protected and internal have no meaning in IL and are not used in the Reflection APIs. The corresponding terms in IL are Family and Assembly. To identify an internal method using Reflection, use the IsAssembly property. To identify a protected internal method, use the IsFamilyOrAssembly.
use internal class in public method
If you just want outside user not to be able to change properties, make the setters internal:
public bool IsExistNewVersion { get; internal set; }
This way everyone can read, but you can only write from inside the assembly (your library).
EDIT
If you don't want users of your library to create an instance of your class, limit access to the constructor:
public class GithubReleaseModel
{
internal GithubReleaseModel()
{
// internal constructor
}
public bool IsExistNewVersion { get; set; }
public string Url { get; set; }
public string Changelog { get; set; }
public string Version { get; set; }
public string DownloadUrl { get; set; }
public string Size { get; set; }
public bool IsPreRelease { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime PublishedAt { get; set; }
}
public static GithubReleaseModel IsNewVersionExistGithub(string Username, string Repository)
{
var model = new GithubReleaseModel();
...
return model;
}
This way you can create and return your model just as you did already, but if someone from outside wants to create a new GithubReleaseModel()
he gets an access violation.
Related Topics
Combining Two Lambda Expressions in C#
What Requirement Was the Tuple Designed to Solve
How to Catch a Specific SQLexception Error
Most Efficient Way to Insert Rows into MySQL Database
Post Form Data Using Httpwebrequest
Wpf Binding a Listbox to an Enum, Displaying the Description Attribute
How to Restart a Wpf Application
C# Console Receive Input with Pipe
"Could Not Load Type [Namespace].Global" Causing Me Grief
Hashtable with Multidimensional Key in C#
"Updatesourcetrigger=Propertychanged" Equivalent for a Windows Phone 7 Textbox
How to Convert Ticks to a Date Format
How to Upload a File to a Document Library in Sharepoint