Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restricting use of a structure in C#

Ok so lets say I have a structure A like that:

Struct A{
    private String _SomeText;
    private int _SomeValue;

    public A(String someText, int SomeValue) { /*.. set the initial values..*/ }

    public String SomeText{ get { return _SomeText; } }
    public int SomeValue{ get { return _SomeValue; } }
}

Now what I want to be able to do is to return that Structure A as a result of a method in a Class ABC, like that:

Class ABC{
    public A getStructA(){
        //creation of Struct A
        return a;
    }
}

I don't want any programmer using my library (which will have Struct A and Class ABC and some more stuff) to ever be able to create an instance of Struct A.
I want the only way for it to be created is as a return from the getStructA() method. Then the values can be accessed through the appropriate getters.

So is there any way to set a restrictions like that? So a Structure can't be instantiated outside of a certain class? Using C#, .Net4.0.

Thanks for your help.

---EDIT:----
To add some details on why am I trying to achieve this:

  • My class ABC has some "status" a person can query. This status has 2 string values and then a long list of integers.
  • There never will be a need to create an object/instance of "Status" by the programmer, the status can only be returned by "getStatus()" function of the class.
  • I do not want to split these 3 fields to different methods, as to obtain them I am calling Windows API (p/invoke) which returns similar struct with all 3 fields.
  • If I was indeed going to split it to 3 methods and not use the struct, I would have to either cache results or call the method from Windows API every time one of these 3 methods is called...

So I can either make a public struct and programmers can instantiate it if they want, which will be useless for them as there will be no methods which can accept it as a parameter. Or I can construct the library in such a way that this struct (or change it to a class if it makes things easier) can be obtained only as a return from the method.

like image 239
Daniel Gruszczyk Avatar asked Dec 13 '25 13:12

Daniel Gruszczyk


1 Answers

If the "restricted" type is a struct, then no, there is no way to do that. The struct must be at least as public as the factory method, and if the struct is public then it can be constructed with its default constructor. However, you can do this:

public struct A
{
    private string s;
    private int i;
    internal bool valid;
    internal A(string s, int i)
    {
        this.s = s;
        this.i = i;
        this.valid = true;
    } 
    ...

and now you can have your library code check the "valid" flag. Instances of A can only be made either (1) by a method internal to your library that can call the internal constructor, or (2) by the default constructor. You can tell them apart with the valid flag.

A number of people have suggested using an interface, but that's a bit pointless; the whole point of using a struct is to get value type semantics and then you go boxing it into an interface. You might as well make it a class in the first place. If it is going to be a class then it is certainly possible to make a factory method; just make all the ctors of the class internal.

And of course I hope it goes without saying that none of this gear should be used to implement code that is resistant to attack by a fully-trusted user. Remember, this system is in place to protect good users from bad code, not good code from bad users. There is nothing whatsoever that stops fully trusted user code from calling whatever private methods they want in your library via reflection, or for that matter, altering the bits inside a struct with unsafe code.

like image 164
Eric Lippert Avatar answered Dec 15 '25 04:12

Eric Lippert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!