access-modifiers

Security impact of access modifiers (public, private, internal, protected)

旧巷老猫 提交于 2020-01-13 09:14:29
问题 Do the access modifiers of classes , properties or methods in C#, Java and other programming languages actually have an impact on the security of an application? Do they also protect against unauthorized access in some way? Or are they just a tool for clear and propper programming? 回答1: No, access modifiers don't offer security protection. They are merely there for developer convenience, e.g. they help to enforce good coding practices and help with programming patterns. It's easy to access

Detect access modifier type on a property using Reflection

天涯浪子 提交于 2020-01-12 06:42:15
问题 I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection. However I need to find out if the property is public or protected. eg: public string Name{get;set;} protected int Age{get;set;} The PropertyInfo class does not seem to expose this information about the property. Is there another way to do this? 回答1: Since properties are just syntactic sugar over a pair of get / set methods, there's no such thing as

Detect access modifier type on a property using Reflection

孤人 提交于 2020-01-12 06:40:06
问题 I have written some code to look at properties using reflection. I have retrieved a list of properties from the class using reflection. However I need to find out if the property is public or protected. eg: public string Name{get;set;} protected int Age{get;set;} The PropertyInfo class does not seem to expose this information about the property. Is there another way to do this? 回答1: Since properties are just syntactic sugar over a pair of get / set methods, there's no such thing as

GetProperty Method returns null C# .Net

十年热恋 提交于 2020-01-07 05:04:21
问题 Can someone please explain, why does the GetProperty method in System.Type returns null for properties that are declared as 'internal' but works for 'public'. internal class Test{ public string ALocal { get; set; } internal string SLocal { get; set; }} var test = new Test(); var testType = test.GetType(); var aProp = testType.GetProperty("ALocal"); => returns string Type var sProp = testType.GetProperty("SLocal"); => returns null I understand differences between internal or public modifiers.

Whether to use private modifier for all fields in a standalone swing applicaton?

强颜欢笑 提交于 2020-01-06 08:09:00
问题 I have a standalone Swing application with a main class which I will call MainClass. The only access to this MainClass is from the main(String args[]) method of this class. In other words, this class is not going to be used by any other class. Should I make the member fields of MainClass "private"? I argue that it is redundant since no other class is going to instantiate MainClass, but another more experienced programmer argues that it violated Java best practices to not label a field

Is there an access modifier that limits to a solution?

巧了我就是萌 提交于 2020-01-05 16:29:30
问题 In my .NET solution, I have two projects: one main project and a project for running tests against the main project. In my project, I have several methods that I'd like to keep "private", but would also like to run tests for. Is there an access method that could limit these functions to just inside of my solution? 回答1: You are looking for the InternalsVisibleTo attribute. This attributes lets you specify other assemblies that should have access to types and methods that are internal to your

Why am I getting an “inaccessible due to protection level” error?

本小妞迷上赌 提交于 2020-01-05 00:40:50
问题 I am getting this error: 'CTest.A.A()' is inaccessible due to its protection level. when compiling this code: public class A { private A() { } } public class B : A { public void SayHello() { Console.WriteLine("Hello"); } } Can anyone explain why? 回答1: Because the default constructor for A is private, try protected A() {} as the constructor. Class B automatically calls the default constructor of A , if that is inaccessible to B or there is no default constructor (if you have constructor

Inconsistent accessibility with abstract classes

China☆狼群 提交于 2020-01-03 21:03:23
问题 I have an internal abstract class InternalClassBase and two (also internal) classes InternalClass1 and InternalClass2 , which inherit from InternalClassBase . I also have a public abstract class PublicClassBase and two (also public) classes PublicClass1 and PublicClass2 , which inherit from PublicClassBase . The PublicClassBase has a protected member XXX of type InternalClassBase , so both PublicClass1 and PublicClass2 can use it. This is my code: internal abstract class InternalClassBase { }

Controlling read/write access to fields

爷,独闯天下 提交于 2020-01-02 23:00:45
问题 Suppose that we would like to separate out the read and write access in an interface pattern as below. namespace accesspattern { namespace ReadOnly { public interface IA { double get_a(); } } namespace Writable { public interface IA : ReadOnly.IA { void set_a(double value); } } } This is easy to implement: namespace accesspattern { namespace ReadOnly { public class A : IA { protected double a; public double get_a() { return a; } } } namespace Writable { public class A : ReadOnly.A, IA {

The missing “framework level” access modifier

一个人想着一个人 提交于 2020-01-02 02:33:19
问题 Here's the scenario. As a creator of publicly licensed, open source APIs, my group has created a Java-based web user interface framework (so what else is new?). To keep things nice and organized as one should in Java, we have used packages with naming convention org.mygroup.myframework.x, with the x being things like components, validators, converters, utilities, and so on (again, what else is new?). Now, somewhere in class org.mygroup.myframework.foo.Bar is a method void doStuff() that I