C# Extensions this的扩展

本文介绍了一种在Unity中使用自定义扩展方法来简化代码的方法,包括目录创建、组件位置设置及空值检查等实用功能,提高了游戏开发效率。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
namespace LNWY
{
    public static class ClassNullSelf
    {
        public static bool IsNull<T>(this T self) where T : class
        {
            return null == self;
        }
        public static bool IsNotNull<T>(this T self) where T : class
        {
            return null != self;
        }
    }

    public static class IOPathSelf
    {
        public static string PathCreateNull(this string dir)
        {
            if (Directory.Exists(dir) == false)
                Directory.CreateDirectory(dir);
            return dir;
        }
        public static void PathDelete(this string dir)
        {
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir);
            }
        }
        public static void PathDeleteNew(this string dir)
        {
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir);
            }
            Directory.CreateDirectory(dir);
        }
    }
    public static class MonoSelf
    {
        public static T ZeroOne<T>(this T t) where T : Component
        {
            t.transform.localPosition = Vector3.zero;
            t.transform.localEulerAngles = Vector3.zero;
            t.transform.localScale = Vector3.one;
            return t;
        }
        public static T SetLocalPosition<T>(this T t, Vector3 pos) where T : Component
        {
            t.transform.localPosition = pos;
            return t;
        }
    }
}

使用

public class Test : MonoBehaviour
{
    int ty = 50;
    public Transform t1;
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 5200, 45), "text"))
        {
            string tttT = "Assets/Resources";
            tttT.PathCreateNull();
            t1.ZeroOne();
        }
        if (GUI.Button(new Rect(10, ty + 10, 200,45), "text2"))
        {
            t1.SetLocalPosition(new Vector3(100, 100, 1));
        }
    }

}
C# 扩展包含扩展方法、扩展方法链以及标记扩展等不同类型,它们在使用方法和应用场景上各有特点。 ### C# 扩展方法 C# 中的扩展方法允许在不修改原有类定义的情况下,为其添加新的方法,通过静态方法实现,但可以像实例方法一样进行调用,极大地增强了代码的灵活性和可维护性,特别是在处理第三方库或无法直接修改源码的类时尤为有用[^1][^2]。 扩展方法的语法规范要求扩展方法必须在静态类中定义,并且第一个参数需要使用 `this` 关键字来指定要扩展的类型。以下是一个简单的示例代码: ```csharp // 定义一个静态类来包含扩展方法 public static class StringExtensions { // 定义一个扩展方法,用于判断字符串是否为空或空白 public static bool IsNullOrWhiteSpaceExt(this string str) { return string.IsNullOrWhiteSpace(str); } } class Program { static void Main() { string testString = " "; // 像调用实例方法一样调用扩展方法 bool result = testString.IsNullOrEmptyWhiteSpaceExt(); Console.WriteLine(result); } } ``` ### 扩展方法链 扩展方法链是指在一个表达式中连续使用多个扩展方法,从而使代码更加简洁和易读,这种方法在处理集合和其他类型时非常常见。通过链式调用,可以将多个操作串联在一起,形成流畅的代码风格[^3]。 以下是一个扩展方法链的示例: ```csharp using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; // 使用扩展方法链,先筛选大于 2 的数字,再将其乘以 2,最后求和 int result = numbers.Where(n => n > 2).Select(n => n * 2).Sum(); Console.WriteLine(result); } } ``` ### C# WPF 标记扩展 在 WPF 应用编程中,标记扩展是一种特殊的语法,用于在 XAML 中提供属性值。最常用的标记扩展是 `Binding`(用于数据绑定表达式)以及资源引用 `StaticResource` 和 `DynamicResource`。通过使用标记扩展,即使属性通常不支持特性语法,也可以使用特性语法为属性提供值[^4]。 以下是一个 WPF 标记扩展的示例: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <!-- 使用 Binding 标记扩展进行数据绑定 --> <TextBlock Text="{Binding SomeProperty}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </Window> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值