/
smwwiki
/
OneScript_fork
Обзор
Документация
Войти
/
smwwiki
/
OneScript_fork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/OneScript.Core/Values/PropertyValueReference.cs
72 строки
3 KB
Andrei Ovsiankin
Тест доступности ЭтотОбъект только на чтение
02 июн 2024, 21:43
02 июн 2024, 21:43
a38a42d
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using OneScript.Contexts; using OneScript.Exceptions; using ScriptEngine.Machine; namespace OneScript.Values { public class PropertyValueReference : IValueReference { private readonly IRuntimeContextInstance _context; private readonly int _contextPropertyNumber; public PropertyValueReference(IRuntimeContextInstance context, string propertyName) { _context = context; _contextPropertyNumber = context.GetPropertyNumber(propertyName); } public PropertyValueReference(IRuntimeContextInstance context, int propertyId) { _context = context; _contextPropertyNumber = propertyId; } public bool Equals(IValueReference other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; if (!(other is PropertyValueReference prop)) return false; return _context.Equals(prop._context) && _contextPropertyNumber == prop._contextPropertyNumber; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; return obj.GetType() == this.GetType() && Equals((PropertyValueReference)obj); } public override int GetHashCode() { return HashCode.Combine(_context, _contextPropertyNumber); } public IValue Value { get { if (_context.IsPropReadable(_contextPropertyNumber)) return (BslValue)_context.GetPropValue(_contextPropertyNumber); throw PropertyAccessException.PropIsNotReadableException(PropertyName()); } set { if(_context.IsPropWritable(_contextPropertyNumber)) _context.SetPropValue(_contextPropertyNumber, value); else throw PropertyAccessException.PropIsNotWritableException(PropertyName()); } } private string PropertyName() => _context.GetPropertyInfo(_contextPropertyNumber).Name; } }