Check if property exists before accessing or setting it

Previously when accessing/setting a property which does not exist,
there will be segmentation faults
This commit is contained in:
Stephan Sundermann 2013-10-10 20:26:26 +02:00
parent 35a9c16ea7
commit 4d0a5a796b

View File

@ -13,23 +13,43 @@
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace Gst { using System;
using System; using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
namespace Gst {
public class PropertyNotFoundException : Exception {}
partial class Object partial class Object
{ {
[DllImport ("libgobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_object_class_find_property (IntPtr klass, IntPtr name);
bool PropertyExists (string name) {
var ptr = g_object_class_find_property (GType.GetClassPtr (), GLib.Marshaller.StringToPtrGStrdup (name));
var result = ptr != IntPtr.Zero;
GLib.Marshaller.Free (ptr);
return result;
}
public object this[string property] { public object this[string property] {
get { get {
GLib.Value v = GetProperty (property); if (PropertyExists (property)) {
object o = v.Val; GLib.Value v = GetProperty (property);
v.Dispose (); object o = v.Val;
return o; v.Dispose ();
return o;
} else
throw new PropertyNotFoundException ();
} set { } set {
GLib.Value v = new GLib.Value (this, property); if (PropertyExists (property)) {
v.Val = value; GLib.Value v = new GLib.Value (this, property);
SetProperty (property, v); v.Val = value;
v.Dispose (); SetProperty (property, v);
v.Dispose ();
} else
throw new PropertyNotFoundException ();
} }
} }