How to hide away properties of an object?
7 次查看(过去 30 天)
显示 更早的评论
I have a class in Matlab, which has a number of properties. I want to make a pcode to obfuscate content of the class. But I couldn't find any way to hide properties of the class: using struct(obj) all the properties become available. Is there any workaround?
1 个评论
Sean de Wolski
2017-10-6
In addition to struct() you can also call metaclass (or ?classname) on it and get everything that way...
采纳的回答
Guillaume
2017-10-6
编辑:Guillaume
2017-10-6
I don't think there's any way to hide private properties from struct.
The only workaround I could think of would be to store the private properties in another class (handle) and use a persistent containers.Map in a function to access that class. Would be a bit of a hassle, so you need to really want to hide these properties:
classdef ClassWithPropertiesToHide
properties (Access = public) %properties that are public, hence don't need hiding
something;
end
properties (Access = private)
objectID; %will be visible through struct, but useless. Used as key in the container.Maps which unfortunately can't use a class as a key
end
methods (Access = public)
function this = ClassWithPropertiesToHide()
persistent idcount; %unique id for each class instance to be used as key in the map
if isempty(idcount)
idcount = 0;
else
idcount = idcount + 1;
end
this.objectID = idcount;
privateprops = GetPrivateProperties(this); %privateprops is a handle class, so can be modified here
privateprops.someprop = somevalue;
privateprops.someotherprop = someothervalue;
end
function someotherfunction(this)
privateprops = GetPrivateProperties(this); %get private properties
%do something
end
end
methods (Access = private)
function privateprops = GetPrivateProperties(this)
persistent propcontainer;
if isempty(propcontainer)
propcontainer = containers.Map;
end
if ~isKey(propcontainer, this.objectID)
propcontainer(this.objectID) = PrivatePropertiesClass %PrivatePropertiesClass is the class to hold the private properties. Its properties have to be public
end
privateprops = propcontainer(this.objectID);
end
end
end
Edit: The main class probably needs a destructor and a slightly more sophisticated implementation of GetPrivateProperties so that when an instance of the main class is destroyed its private properties are also removed from the map. Otherwise, memory usage can only go up. Left as an exercice to the reader...
5 个评论
Guillaume
2017-10-6
As I tried to show in the code of someotherfunction, it's actually not that difficult to operate on the private properties, you just obtain a reference to the private properties object at the beginning of the function and use the (public) properties of that object as if they were the private properties of your class. Because that private property class is a handle class, you don't have to apply these changes:
function someotherfunction(this)
privateprops = GetPrivateProperties(this);
privateprops.SomeProp = privateprops.SomeProp + 1;
%someprop is permanently update for the current instance. No need to apply back
end
But yes, as I said a bit of a hassle.
A bigger issue is that you also need to overload saveobj and loadobj to actually save the private properties now that they're not actually properties, otherwise they will be lost through save and load.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!