public static class Native
{
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref RECT rectangle);
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hwnd, ref RECT rectangle);
public RECT GetWindowPos(IntPtr hwnd)
{
var r = new RECT();
GetWindowRect(hwnd, ref r);
return r;
}
public RECT GetClientPos(IntPtr hwnd)
{
var r = new RECT();
GetClientRect(hwnd, ref r);
return r;
}
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct RECT(int left, int top, int right, int bottom)
{
public int Left = left;
public int Top = top;
public int Right = right;
public int Bottom = bottom;
public readonly int Width => Right - Left;
public readonly int Height => Bottom - Top;
}
}
RenderSize
и ActualWidth/ActualHeight
будут после вычисления положения самого элемента. Скорее всего вы слишком рано пытаетесь получить к ним доступ. Запустите ваше приложение и в отладчике посмотрите в дереве окон значения.public class MyControl : Control
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
// Размеры и позиция элемента вычислены и он готов к работе
}
}
using System.Reflection;
namespace ConsoleAppTest
{
public static class Program
{
private static void Main(string[] args)
{
Test obj = new() { Prop1 = "Prop1 Value" };
// Получаем тип объекта
Type objType = typeof(Test);
// Получаем список интерфейсов
Type[] interfaces = objType.GetInterfaces();
foreach (Type iface in interfaces)
{
// Получаем список свойств интерфейса
PropertyInfo[] ifaceProperties = iface.GetProperties();
foreach (PropertyInfo prop in ifaceProperties)
{
// Ищем нужный аттрибут в свойстве
Attribute? attribute = prop.GetCustomAttribute<TestAttribute>();
if (attribute != null)
{
// Получаем значение свойства
object? propValue = prop.GetValue(obj); // -> Prop1 Value
}
}
}
}
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class TestAttribute : Attribute
{ }
internal sealed class Test : ITest
{
public string Prop1 { get; set; } = string.Empty;
}
internal interface ITest
{
[Test]
public string Prop1 { get; set; }
}
}
public class Foo
{
public virtual void Update()
{
Console.WriteLine("Foo->Update()");
}
}
public class Bar : Foo
{
public override void Update()
{
base.Update();
Console.WriteLine("Bar->Update()");
}
}
SelectedItem
SelectionChanged
object
и отдельно его тип. Либо реализовать поддержку типов всех возможных аргументов. А если еще подумать, то можно заменить большую часть вот этого кода обычным словарём. ///
///
///
/// Initializes a new instance of the Bitmap class with the specified
/// size and format.
///
///
public Bitmap(int width, int height, PixelFormat format) {
IntPtr bitmap = IntPtr.Zero;
int status = SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(width, height, 0, (int) format, NativeMethods.NullHandleRef, out bitmap);
if (status != SafeNativeMethods.Gdip.Ok)
throw SafeNativeMethods.Gdip.StatusException(status);
SetNativeImage(bitmap);
}
///
///
/// Initializes a new instance of the
///
/// class with the specified size.
///
public Bitmap(int width, int height) : this(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb) {
}
dynamic
же есть:string json = @"[
{
'Title': 'Json.NET is awesome!',
'Author': {
'Name': 'James Newton-King',
'Twitter': '@JamesNK',
'Picture': '/jamesnk.png'
},
'Date': '2013-01-23T19:30:00',
'BodyHtml': '<h3>Title!</h3>\r\n<p>Content!</p>'
}
]";
dynamic blogPosts = JArray.Parse(json);
dynamic blogPost = blogPosts[0];
string title = blogPost.Title;
Console.WriteLine(title);
// Json.NET is awesome!
string author = blogPost.Author.Name;
Console.WriteLine(author);
// James Newton-King
DateTime postDate = blogPost.Date;
Console.WriteLine(postDate);
// 23/01/2013 7:30:00 p.m.
dynamic product = new JObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
product.Tags = new JArray("Real", "OnSale");
Console.WriteLine(product.ToString());
// {
// "ProductName": "Elbow Grease",
// "Enabled": true,
// "Price": 4.90,
// "StockCount": 9000,
// "StockValue": 44100,
// "Tags": [
// "Real",
// "OnSale"
// ]
// }
public class BarType
{ }
public class Foo
{
public static string Bar<T>() => $"Bar() is called with generic: {typeof(T).FullName}";
}
Type type = typeof(Foo);
MethodInfo mi = type.GetMethods().Single(m => m.Name == "Bar" && m.IsGenericMethodDefinition);
MethodInfo genericMi = mi.MakeGenericMethod(typeof(BarType));
object result = genericMi.Invoke(null, []);
Console.WriteLine($"Result: {result}");
>> Result: Bar() is called with generic: Example.App+BarType